Two things: first, Array.find() returns the first matching element, undefined if it finds nothing. Array.filter returns a new array containing all matching elements, [] if it matches nothing.
Second thing, if you want to match 4,5, you have to look into the string instead of making a strict comparison. To make that happen we use indexOf which is returning the position of the matching string, or -1 if it matches nothing.
Example:
const arr = [
{
name: 'string 1',
arrayWithvalue: '1,2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2,3',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4,5',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4',
other: 'that',
},
];
const items = arr.filter(item => item.arrayWithvalue.split(',').indexOf('4') !== -1);
console.log(items);
Answer from Orelsanpls on Stack OverflowTwo things: first, Array.find() returns the first matching element, undefined if it finds nothing. Array.filter returns a new array containing all matching elements, [] if it matches nothing.
Second thing, if you want to match 4,5, you have to look into the string instead of making a strict comparison. To make that happen we use indexOf which is returning the position of the matching string, or -1 if it matches nothing.
Example:
const arr = [
{
name: 'string 1',
arrayWithvalue: '1,2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '2,3',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4,5',
other: 'that',
},
{
name: 'string 2',
arrayWithvalue: '4',
other: 'that',
},
];
const items = arr.filter(item => item.arrayWithvalue.split(',').indexOf('4') !== -1);
console.log(items);
Use array filter method. Like
arr.filter(res => res.arrayWithvalue.split(',').indexOf('4') !== -1);
Finding the array element:
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find(o => o.name === 'string 1');
console.log(obj);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Replacing the array element:
let arr = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
let obj = arr.find((o, i) => {
if (o.name === 'string 1') {
arr[i] = { name: 'new string', value: 'this', other: 'that' };
return true; // stop searching
}
});
console.log(arr);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can loop over the array and test for that property:
function search(nameKey, myArray){
for (let i=0; i < myArray.length; i++) {
if (myArray[i].name === nameKey) {
return myArray[i];
}
}
}
const array = [
{ name:"string 1", value:"this", other: "that" },
{ name:"string 2", value:"this", other: "that" }
];
const resultObject = search("string 1", array);
console.log(resultObject)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
javascript - How to get list of all objects in an array of objects? - Stack Overflow
javascript - Return a property from all matching elements in an array of objects? - Stack Overflow
How to find object in array of objects, who's nested array matches all the objects in a separate array?
Trying to diff array of objects with Lodash in Angular
You want an array C which contains all the objects from array A which weren't in array B? And strict equality won't work?
C = A - B:
let a = [{x: 0}, {x: 1}, {x: 2}, {x: 3}];
let b = [{x: 1}, {x: 2}];
let c = a.filter(item => !b.some(other => item.x === other.x));
console.log(c); // [{x:0}, {x: 3}]
Something like that, I guess.
By the way, next time, put a test case on JSFiddle, JSBin, or CodePen. Also, remove all unnecessary details like that it's about users and security and whatever. Remove everything which isn't relevant.
More on reddit.comVideos
First of all you need to rotate your loop of array then you need to rotate your json loop.
const obj = [{
a:'a',
b:'b',
},
{
c:'c',
d:'d',
},
{
e:'e',
f:'f',
}]
let keys = [];
for(let i= 0; i < obj.length; i++){
for(let key in obj[i]){
keys.push(key);
}
}
console.log(keys);
const obj = [{
a:'a',
b:'b',
},
{
c:'c',
d:'d',
},
{
e:'e',
f:'f',
}]
const res = obj.map(a=> Object.values(a)).flat()
console.log(res)
You should invoke the Array.prototype.filter function there.
var filteredArray = YourArray.filter(function( obj ) {
return obj.value === 1;
});
.filter() requires you to return the desired condition. It will create a new array, based on the filtered results. If you further want to operate on that filtered Array, you could invoke more methods, like in your instance .map()
var filteredArray = YourArray.filter(function( obj ) {
return obj.value === 1;
}).map(function( obj ) {
return obj.id;
});
console.log( filteredArrays ); // a list of ids
... and somewhere in the near future, we can eventually use the Arrow functions of ES6, which makes this code even more beauty:
var filteredArray = YourArray.filter( obj => obj.value === 1 ).map( obj => obj.id );
Pure JS.... no filter/map functions, that are not available for IE < 9
var array = [
{id:10, value:2}, {id:11, value:1}, {id:12, value:3}, {id:13, value:1}
],
result = [];
for(key in array) { if (array[key].value == 1) result.push(array[key].id); }
I have an array of options and an array of productVariants. I would like to find the productVariant who's selectedOptions property matches all the name and value fields in an option array?
Types would look like this
const options: Array<{name: String, value: String}> const productVariants: Array<{selectedOptions: Array<{name: String, value: String, optionalFields?: String}>}> Example of the data here:
const options = [{ name: “Format”, value: “Hardback” }, { name: “Colour”, value: “Pink” }, { name: “Size”, value: “Large” }] const productVariants = [{selectedOptions: [{name: "Format", value: "Zine", optionalFields: “abc”},{name: "Colour", value: "Grey", optionalFields: “abc”}]}, {selectedOptions: [{name: “Format”, value: "Zine", optionalFields: “abc”},{name: “Colour”, value: “blue”, optionalFields: “abc”}]}] My desired output would be to retrive a single productVariant object who's selectedOptions array matches the options Array.
Thanks for the help!