Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
lets say we have an array of objects:
results = [
{id: 1, name: John},
{id: 2, name: Gabo},
{id: 1, name: Anna},
{id: 3, name: Gabo}
{id: 1, name: Jack}, ]
I want function which gets all this objects which has unique id and name and it's value is not in other object.
results = [
{id: 1, name: John}, // unique id-name so we add to array
{id: 2, name: Gabo}, // unique id-name so we add to array
{id: 1, name: Anna}, // name is unique but object with this id already exists in array so we reject
{id: 3, name: Gabo} // id is unique but object with this name already exists in array so we reject
{id: 1, name: Jack}, ] //etc..
results = [
{id: 1, name: John},
{id: 2, name: Gabo},
You can use Set to register and then quickly check for duplicate id or name:
function getUniq(items) {
const ids = new Set();
const names = new Set();
return items.filter((item) => {
if (ids.has(item.id) || names.has(item.name)) {
return false;
}
ids.add(item.id);
names.add(item.name);
return true;
});
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have an array like this.
[{id: 1, name: "john", item:['a']},
{id: 1, name: "john", item:['b']},
{id: 2, name: "linda", item:['c']},
{id: 2, name: "linda", item:['d']},
{id: 3, name: "liam", item:['e']}
]
I hope to get grouped arrays having same id and name value like this
[{id: 1, name: "john", item:['a']},
{id: 1, name: "john", item:['b']}
],
[{id: 2, name: "linda", item:['c']},
{id: 2, name: "linda", item:['d']}
],
[{id: 3, name: "liam", item:['e']}
]
Please help me
One solution could be to store the items in a map (for simplicity an object) based on the id and then get the values from that object.
First, we have to check if we already have values stored for that id if that is the case we push the value to the array, if no we have to create the array.
const objMap = {};
items.forEach(item => {
const {id} = item;
if(objMap[id]) objMap[id].push(item);
else objMap[id] = [item];
})
const groupedValues = Object.values(objMap)
I hope that this solves your problem.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Given two arrays of the same length and identical content, how can one sort an array to be the in same order as the second array, based on a shared property?
Example:
let array1 = [{id: 123, ...}, {id: 456, ...}, {id: 789, ...}] // order always stays the same
let array2 = [{id: 456, ...}, {id: 789, ...}, {id: 123, ...}] // order is always different
How can I sort array1 such that:
array1[0].id is 456 and
array2[0].id is 456
Loop through the first and push to the second by index:
let arr1 = [{id: 123}, {id: 456}, {id: 789}] // order always stays the same
let arr2 = [{id: 456}, {id: 789}, {id: 123}]
arr1.forEach((obj1, idx) => {
arr2[idx] = obj1
})
console.log(arr1, arr2)
Assuming array2 has the objects we care about and array1 specifies the order, map over the ordering array using it to select the objects to be ordered...
let array1 = [{id: 123 }, {id: 456 }, {id: 789, }]
let array2 = [{id: 456, name: '456' }, {id: 789, name: '789' }, {id: 123, name: '123' }]
let array2ElementsSortedByArray1 = array1.map(e => {
return array2.find(e2 => e2.id === e.id)
})
console.log(array2ElementsSortedByArray1)
You can create a hash of the shape {id: index, ...} from array2 and then use this to order the elements of array1. This saves you the cost of find() on each iteration.
let array1 = [{ id: 123, }, { id: 456, }, { id: 789, }] // order always stays the same
let array2 = [{ id: 456, }, { id: 789, }, { id: 123, }]
const
indeces = Object.fromEntries(array2.map(({ id }, i) => [id, i])), // { 456: 0, 789: 1, 123: 2 }
ordered = [];
array1.forEach(o => ordered[indeces[o.id]] = { ...o });
console.log(ordered)
I am having the array of objects like below
Array 1:
[{id: 1, name: 'Golden', isEdited: true}, {id: 2, name: 'Pearl'}]
Array 2:
[{id: 1, name: 'Golden'}, {id: 2, name: 'Pearlblue'}, , {id: 3, name: 'Orange'}]
Now i would like to merge the two arrays if the object contains isEdited flag means then that object should not be updated.
Expected result should be
[{id: 1, name: 'Golden', isEdited: true}, {id: 2, name: 'Pearlblue'}, {id: 3, name: 'Orange'}]
I have tried with the below approach
b.map((battr) => {
return {
...a[battr.id],
...battr
}
})
But it returns the output as
[{id: 1, name: 'Golden'}, {id: 2, name: 'Pearlblue'}, {id: 3, name: 'Orange'}]
Be carefull, your ids don't match your array indexes.
Hence in your attempt a[battr.id] should be replaced by something like a.find(a => a.id === battr.id)
Your example is confusing since in both arrays the name is 'Golden' for id=1, but assuming you want only the properties from array A if isEdited=true, your solution could be :
b.map((b_attr) => {
var matchingA = a.find(a_attr => a_attr.id === b_attr.id);
if (matchingA && matchingA.isEdited)
return matchingA;
else
return { ...matchingA, ...b_attr }
})
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);
This question already has answers here:
Create object from array
(6 answers)
Closed 4 years ago.
I have array of objects:
const objects = [
{id: 2, name: "aaa"},
{id: 4, name: "bbb"},
{id: 11, name: "ccc"},
{id: 21, name: "ddd"}
];
Is any option to do new object based on ids from objects?
I would like to receive:
const list = {
2: false,
4: false,
11: false,
21: false
};
I can iterate through objects but maybe exists better way?
I like to use array.reduce for this. Reduce takes an array and "reduces" it to some other value.
var byId = objects.reduce(function(map, obj){
map[obj.id] = obj;
return map;
}, {})