What is the best way add all array item in Javascript [duplicate] - javascript

This question already has answers here:
Merge/flatten an array of arrays
(84 answers)
Closed 1 year ago.
const arr1 = [
[{ id: 1 }],
[{ id: 2 },{ id: 3 }],
[{ id: 4 }]
];
I want to add all items in this array how can I do this. I want my output as Like:
const arr2 = [
{id:1},
{id:2},
{id:3},
{id:4}
]

Using arr1.flat() will work for your example. If you have an array of even greater depth of nested arrays of objects, you can even use arr1.flat(Infinity) to flatten to a variable depth:
const arr1 = [
{ id: 0 },
[{ id: 1 }],
[{ id: 2 },{ id: 3 }],
[{ id: 4 }]
];
console.log(arr1.flat());

Related

Map and sort two arrays into one [duplicate]

This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Javascript group objects by property [closed]
(3 answers)
Closed 2 years ago.
I have an object which has two arrays like:
let myData= {
firstArray: [],
seccondArray: []
};
firstArray is an Array which has some data with unique id's like:
firstArray = [
0: { id: 1, name: name1 },
1: { id: 2, name: name2 }
...]
seccondArray is another Array which has some data like:
seccondArray = [
0: { itemId: 1, nameForItem: name1, optionalProperty: property1 },
1: { itemId: 2, nameForItem: name2 },
2: { itemId: 3, nameForItem: name3 },
3: { itemId: 1, nameForItem: name4 }
...]
So I need to create finalArray which will have id and items array inside, where inside items will be pushed all items from seccondArray where the itemId = firstArray.id.
id is the unique id taken firstArray.id. Also the element should be mapped with all its other properties like item1 has for example optionalProperty: property1, so finalArray looks like:
finalArray = [
{ id: 1,
items: [
0: {itemId: 1, nameForItem: name1 , optionalProperty: property1},
1: {itemId: 1, nameForItem: name4}
],},
{id: 2, items: [{itemId: 2, nameForItem: name2}]},
{id: 3, items: [{itemId: 3, nameForItem: name3}]},
{id: 4, items: [{itemId: 4, nameForItem: name4}]}
];

JavaScript get the element in array of object [duplicate]

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)

How to avoid override of a list element in javascript? [duplicate]

This question already has answers here:
How do I correctly clone a JavaScript object?
(81 answers)
Do objects pushed into an array in javascript deep or shallow copy?
(2 answers)
Closed 2 years ago.
If you run the code snippet below you can observe the following:
at first console.log() it print 0,Jhon after I modified the first element of the first array the same change is also reversed on the object in the second list, why?
its possible to avoid this?
This is a sample of this problem:
var myArray = [{
id: 0,
name: "Jhon"
},
{
id: 1,
name: "Sara"
},
{
id: 2,
name: "Domnic"
},
{
id: 3,
name: "Bravo"
}
];
var a = [];
a.push(myArray[0]);
console.log(a);
myArray[0].name = 'TEST';
console.log(a);
If you dont want it to be by reference, you can use spread syntax
a.push({...myArray[0]});
complete code:
var myArray = [{
id: 0,
name: "Jhon"
},
{
id: 1,
name: "Sara"
},
{
id: 2,
name: "Domnic"
},
{
id: 3,
name: "Bravo"
}
];
var a = [];
a.push({...myArray[0]});
console.log(a);
myArray[0].name = 'TEST';
console.log(a);

How to create array of objects with unique key/values pairs from another array of objects?

I'm trying to achieve from this array of objects one smaller array of objects with unique key/value pairs.
This is the array from which I want to get a group and its value.
This is the main array of objects:
let samples = {
{
id: 2,
group: 'picturesWithDescription',
elements: [
{
id: 1
},
{
id: 2
}
]
},
{
id: 3,
group: 'footers',
elements: [
{
id: 1
},
{
id: 2
}
]
},
{
id: 4,
group: 'movies',
elements: [
{
id: 1
},
{
id: 2
}
]
},
{
id: 4,
group: 'movies',
elements: [
{
id: 1
},
{
id: 2
}
]
}
}
And this is something I would like to achieve:
let result = [
{group: 'picturesWithDescription'},
{group: 'footers'},
{group: 'movies'}
]
Assuming that samples is an array as said in question.
Try following
let samples = [{id:2,group:'picturesWithDescription',elements:[{id:1},{id:2}]},{id:3,group:'footers',elements:[{id:1},{id:2}]},{id:4,group:'movies',elements:[{id:1},{id:2}]},{id:4,group:'movies',elements:[{id:1},{id:2}]}];
/* 1. Create an array with only group names
** 2. Convert it into Set to remove duplicates.
** 3. Convert back to array.
** 4. Create an array with object with group property. */
let result = [...new Set(samples.map(({group}) => group))].map((group) => {return {group}});
console.log(result);
OR
let samples = [{id:2,group:'picturesWithDescription',elements:[{id:1},{id:2}]},{id:3,group:'footers',elements:[{id:1},{id:2}]},{id:4,group:'movies',elements:[{id:1},{id:2}]},{id:4,group:'movies',elements:[{id:1},{id:2}]}];
/* 1. Reduce the array into an object with group names as key (removes duplicate)
** 2. Iterate over the keys (group names) and create an object with group property*/
let result = Object.keys(samples.reduce((a,{group}) => {a[group] = group; return a}, {})).map((group) => {return {group}});
console.log(result);

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

Categories