Hi everyone! General Javascript question. Without ...
# suitescript
k
Hi everyone! General Javascript question. Without any external libraries, what is the most efficient way to get an array of keys from an array of objects for all objects with a given property value?
s
So you have an array of objects, and you want to know which objects in that array posses a particular key, right?
k
Almost
I have an array of objects, and I want the array indexes of the objects with a property matching a value
s
Ok you wanna look at that property of each object and compare it to some value, and know the indexes that is true
k
yeah
Basically array.filter but returning the indexes instead of the values
s
Something close to this?
Copy code
var testValue = 'x';
var testProperty = 'y';
objectArr.map(function (entry, index){
    if (entry[testProperty] == testValue){
        return index;
    }
});
k
More specifically each array element is a sales order item line. I'm doing a saved search of the items in the array to get the text values of their itemid (getting the field text only returns the ID).
For each search result I want to find the array indexes for the items matching that result, and update the object property for the itemid text
I think map will be perfect! šŸ™‚
I'll try that now.
s
you prob ened to run a .filter on it to remove
undefined
Copy code
let testArr = [
    {name: 'a'}, {name: 'a'}, {}
]
testArr.map(function (entry, index){
    if (entry.name == 'a')
        return index;
})
//results
Ā [0, 1, undefined]
k
I'll try that thanks
s
I'm genuinely curious to learn the reasons behind folks choosing to reinvent the wheel rather than use external libraries?
s
I use lodash or moment in so many scripts, really don't see a reason to not use them but to each their own.
s
yes, I tend to avoid reinventing the wheel whenever I can so I'm curious as to why @Kris Wood intentionally added 'without an external library' to his initial question.
k
I just don't want to learn a new library / add a library to the customer's NetSuite when resolving a Usage Limit Exceeded error that's blocking business
s
ah, learning something new while in firefighting mode would be painful indeed!
k
I'll get around to learning it when it's lower priority. šŸ™‚
I need to get around to learning N/query as well eventually too but I'm always putting out fires
s
interestingly, sometimes those external libs will actually help create designs with less governance
k
right now the script is doing a search for each item line and I'm trying to condense it down to one search for all the item lines at once
yup, threw an error on
undefined
just like you said
adding that in now
s
that sounds like a great improvement