New object based on list of values from objects [duplicate] - javascript

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;
}, {})

Related

How to sort an Array of objects in Javascript [duplicate]

This question already has answers here:
Sort array of objects by string property value
(57 answers)
Closed 11 months ago.
I have this array:
array: [
{id: 1, title: 'Thanos', content: '123'},
{id: 2, title: 'Deadpool', content: '456'},
{id: 3, title: 'Batman', content: '789'}
]
and I want it to be sorted by title key. How do I do that in Javascript?
array.sort((a, b) => a.title.localeCompare(b.title));

Unique objects from array based on two properties javascript [closed]

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;
});
}

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);

JS Search in object array by using array [duplicate]

This question already has answers here:
Filter array of objects based on another array in javascript
(10 answers)
Closed 3 years ago.
I want to remove all the object from a data array that contains the same id in an array of id. How can I achieve this task without looping it?
const id = [1, 2];
const data = [
{id: 1},
{id: 2},
{id: 3}
];
console.log(data);
You can try with Array.prototype.filter()
The filter() method creates a new array with all elements that pass the test implemented by the provided function.
and Array.prototype.includes():
The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.
const id = [1, 2];
const data = [
{id: 1},
{id: 2},
{id: 3}
];
var res = data.filter(i => !id.includes(i.id));
console.log(res);
let newData = data.filter(item => !id.includes(item.id));
console.log(newData);
You can use .filter() and .includes() for filtering your object.
const id = [1, 2];
let data = [
{id: 1},
{id: 2},
{id: 3}
];
data = data.filter((item) => (!id.includes(item.id)));
console.log(data);
You can use method uniqBy from lodash https://lodash.com/docs/4.17.11#uniqBy
const uniqArray = _.uniqBy([{ 'id': 1 }, { 'id': 2 }, { 'id': 1 }], 'id');
console.log(uniqArray)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

how to prevent sequence when i use Array.forEach [duplicate]

This question already has answers here:
Sort JavaScript object by key
(37 answers)
Closed 6 years ago.
const a = {};
array.forEach(item => {
a[item.id] = item;
})
when i get a, I found it was sort by item.id. How to prevent the sort when forEach.
if array = [{id: 2}, {id: 6}, {id : 1}], and then I get a = {1: {id: 1}, 2: {id: 2}, 6: {id: 6}}.
my want is a={2: {id: 2}, 6: {id:6}, 1: {id: 1}}
I don't think you can enforce a particular object key sequence in JS. You can, however, create an array of the keys.
const a = {};
const originalSequence = [];
array.forEach(item => {
a[item.id] = item;
originalSequence.push(item.id);
})

Categories