JavaScript get the element in array of object [duplicate] - javascript

This question already has answers here:
How to find object in array by property in javascript?
(3 answers)
Closed 2 years ago.
I have 2 Array:
const arr1 = [
{
id: 1,
name: "a"
},
{
id: 2,
name: "ab"
},
{
id: 3,
name: "abc"
}]
and
const arr2 = [{id:"1"}, {id:"3"}]
How can i get from two above array to get the result like that:
const result = ["a", "abc"]
I'm struggling with array built-in function. Thank you for reading.

You could do something like the following.
const arr1 = [{ id: 1, name: "a"}, {id: 2, name: "ab"}, { id: 3, name: "abc" }]
const arr2 = [{ id: 1 }, { id: 3 }];
const ids = arr2.map(item => item.id);
const includedIds = arr1.filter(item => ids.includes(item.id)).map(item => item.id)
console.log(includedIds)

Related

filter array of object using an array [duplicate]

This question already has an answer here:
Filter array of object from another array
(1 answer)
Closed 3 years ago.
I want to filter an array of objects using an array but I want the results on the basis of array index and the result should be repeated when the array index value is repeated.
const data = [{
id='1',
name:'x'
},
{
id='4',
name:'a'
},
{
id='2',
name:'y'
},
{
id='3',
name:'z'
}
]
cons idArray = [1,4,3,2,4,3,2]
I have tried following code and get the result only once
const filteredData = data.filter(arrayofObj => idArray.includes(arrayofObj.id))
console.log(filteredData)
expected output is
expected output is =
[{id = '1,name:'x'},{id='4',name:'a'},{
id='3',
name:'z'
},
{
id='2',
name:'y'
},{
id='4',
name:'a'
},
{
id='3',
name:'z'
},{
id='2',
name:'y'
}]
First convert data array into Object with id's as keys.
Second, use map method over idArray and gather objects from above object.
const data = [
{
id: "1",
name: "x"
},
{
id: "4",
name: "a"
},
{
id: "2",
name: "y"
},
{
id: "3",
name: "z"
}
];
const dataObj = data.reduce((acc, curr) => {
acc[curr.id] = { ...curr };
return acc;
}, {});
const idArray = [1, 4, 3, 2, 4, 3, 2];
const results = idArray.map(id => ({ ...dataObj[id] }));
console.log(results);
You could map with a Map.
const
data = [{ id: '1', name: 'x' }, { id: '4', name: 'a' }, { id: '2', name: 'y' }, { id: '3', name: 'z' }],
idArray = [1, 4, 3, 2, 4, 3, 2],
result = idArray.map(Map.prototype.get, new Map(data.map(o => [+o.id, o])));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Function question, angular typescript, match id values from 2 arrays to get the index from one of them

I have a two arrays, and I want to match their ID values and then get the index of that id in the second array. I know this sounds simple but I'm super new to the syntax, and I'm having a hard time picturing what this should look like. can anyone model that in simple terms?
example functionality:
var array1 = { id:2, name: 'preston'}
array2 = {
{
id: 1
name: 'john'
},
{
id: 2
name: 'bob'
}
Expected behavior
where both ids = 2, give index of array2.
returns 1
can anyone show me?
You can use findIndex on array2
Try this:
var array1 = {
id: 2,
name: 'preston'
}
var array2 = [{
id: 1,
name: 'john'
},
{
id: 2,
name: 'bob'
}
]
console.log(array2.findIndex(item => item.id === array1.id))
Or use indexOf with map if you want support for IE as well without polyfills.
var array1 = {
id: 2,
name: 'preston'
}
var array2 = [{
id: 1,
name: 'john'
},
{
id: 2,
name: 'bob'
}
]
console.log(array2.map(item => item.id).indexOf(array1.id))
Iterate over each item in array1 using forEach(). Find each item's index in array2 using findIndex().
var array1 = [{id:2, name: "preston"}];
var array2 = [{id: 1, name: "john" }, {id: 2, name: "bob" }];
array1.forEach(item => {
let index = array2.findIndex(obj => obj.id === item.id);
console.log(index);
});

Javascript remove array from object where id [duplicate]

This question already has answers here:
How to filter object array based on attributes?
(21 answers)
Closed 4 years ago.
Sorry for my bad English. If I have an array:
const myobj = [
{
id: 1,
name: 'First...'
},
{
id: 2,
name: 'Second...
}];
How can I remove, for example, the object with id 2? To leave the array only with first object. Which functions should I use? Thanks in advance.
Found solution:
function removeByKey(array, params){
array.some(function(item, index) {
if(array[index][params.key] === params.value){
array.splice(index, 1);
return true;
}
return false;
});
return array;
}
Then
removeByKey(myobj, {
key: 'id',
value: 2
})
http://jsforallof.us/2015/07/08/remove-object-by-key-from-array/
Use Array.prototype.filter which produces new array based on the provided condition.
const myobj = [{ id: 1, name: 'First...' }, { id: 2, name: 'Second...' }];
console.log(myobj.filter(v => v.id !== 2));
Similarly you can use Array.prototype.reduce
const myobj = [{ id: 1, name: 'First...' }, { id: 2, name: 'Second...' }];
console.log(myobj.reduce((acc, v) => v.id !== 2 ? acc.concat(v) : acc, []));
Using filter and assuming myobj is not constant you can do the following:
myobj = [
{
id: 1,
name: 'First...'
},
{
id: 2,
name: 'Second...'
},
{
id: 3,
name: 'Third...'
}
];
myobj = myobj.filter(v => v.id !== 2);
console.log(myobj);
first, you have an array, not an object in myobj, one way of many you could do this is to remove the item by filtering:
const myFilteredArray = myobj.filter(i => i.id !== 2);
See the javascript filter function

combining two set of data in javascript [duplicate]

This question already has answers here:
How to merge two arrays in JavaScript and de-duplicate items
(89 answers)
Closed 4 years ago.
Im trying to combine two sets of data.
var a = [{ Id: 1, Name: 'foo' },
{ Id: 2, Name: 'boo' }];
var b = [{ Id: 3, Name: 'doo' },
{ Id: 4, Name: 'coo' }];
Most of question here i found is only a normal array.
I've tried Object.assign(a, b); but it only returns the b value.
The a and b data is from the server side.
Thanks for the help.
Try array concat
var a = [{ Id: 1, Name: 'foo' },
{ Id: 2, Name: 'boo' }];
var b = [{ Id: 3, Name: 'doo' },
{ Id: 4, Name: 'coo' }];
let c = a.concat(b);
console.log(c);
Using spread syntax
var a = [{ Id: 1, Name: 'foo' },
{ Id: 2, Name: 'boo' }];
var b = [{ Id: 3, Name: 'doo' },
{ Id: 4, Name: 'coo' }];
c = [...a, ...b];
Note: Spread syntax is not supported by all browsers , its ok if you use es6/5 compiler though like babel. See Spread
Another option is

Compare two Arrays with Objects and create new array with unmatched objects

I have the following two Javascript arrays:
const array1 = [{ id: 1}, { id: 2 }, { id: 3 }, { id: 4}];
const array2 = [{ id: 1}, { id: 3 }];
I now want a new array array3 that contains only the objects that aren't already in array2, so:
const array3 = [{ id: 2}, { id: 4 }];
I have tried the following but it returns all objects, and when I changed the condition to === it returns the objects of array2.
const array3 = array1.filter(entry1 => {
return array2.some(entry2 => entry1.id !== entry2.id);
});
Any idea? ES6 welcome
You could reverse the comparison (equal instead of unqual) and return the negated result of some.
const
array1 = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }],
array2 = [{ id: 1 }, { id: 3 }],
array3 = array1.filter(entry1 => !array2.some(entry2 => entry1.id === entry2.id));
// ^ ^^^
console.log(array3);
Nina's answer is a good start but will miss any unique elements in array 2.
This extends her answer to get the unique elements from each array and then combine them:
const
array1 = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }],
array2 = [{ id: 1 }, { id: 3 }, { id: 5 }],
array3 = array1.filter(entry1 => !array2.some(entry2 => entry1.id === entry2.id)),
array4 = array2.filter(entry1 => !array1.some(entry2 => entry1.id === entry2.id)),
array5 = array3.concat(array4);
console.log(array5);

Categories