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);
})
Related
This question already has answers here:
Filter object properties by key in ES6
(30 answers)
Closed 3 years ago.
So i want to filter object of objects, and print all of them except one, which i specify by key.
const obj = {1: {age: 10}, 2: {age: 20}};
console.log(obj);
So i must have option to somehow specify that i don't want the obj[1] to be printed. How do i do that? : |
I don't want to delete anything from it.
You could destructure the object:
const obj = {1: {age: 10}, 2: {age: 20}, 3: {age: 30}},
key = 1;
const { [key]:_, ...rest } = obj
console.log(rest);
You can filter() the keys and then convert it back to object using reduce()
const obj = {1: {age: 10}, 2: {age: 20}};
let key = 1
console.log(Object.keys(obj).filter(x => x != key).reduce((ac,a) => ({...ac,[a]:obj[a]}),{}));
Using the new feature Object.fromEntries
const obj = {1: {age: 10}, 2: {age: 20}};
let key = 1
let res = Object.fromEntries(Object.entries(obj).filter(x => x[0] != key));
console.log(res);
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>
This question already has answers here:
JavaScript "new Array(n)" and "Array.prototype.map" weirdness
(14 answers)
Closed 4 years ago.
I have an array of objects that looks like this:
const input = [
{id: 3, value: 2},
{id: 0, value: 3},
{id: 2, value: 8},
{id: 1, value: 5},
{id: 0, value: 2},
{id: 1, value: 6}
]
And I am trying to build an array of the maximum value by id, with id as index. For our example the desired output is the following:
const output = [ 3, 6, 8, 2 ]
I am also assuming that I know the number of unique ids ahead of time, and that they start at 0 and rise sequentially. My first whack at this was to .map() over the an empty array of the right length and build intermediate arrays with .filter() for each id, then use Math.max() on the filtered arrays.
const myAttempt = Array(4).map((_, index) => {
const filtered = input.filter(item => item.id === index);
return Math.max(filtered);
});
All I get out of this is:
myAttempt = [ 4 empty slots ];
I suspect I'm totally off-base with the Array(4) bit, and that the answer might involve .reduce(), but I never really got the hang of reduce so any help would be very much appreciated.
PS: I'd prefer answers that avoid the use of libraries in the vein of lodash or jQuery.
Use Array.reduce() to collect the values highest value of each key. Convert to array using Array.values():
const input = [
{id: 3, value: 2},
{id: 0, value: 3},
{id: 2, value: 8},
{id: 1, value: 5},
{id: 0, value: 2},
{id: 1, value: 6}
]
const result = Object.values(input.reduce((r, { id, value }) => {
r[id] = +r[id] > value ? r[id] : value;
return r;
}, {}));
console.log(result);
If all ids from 0 on wards appear in the array, you can add the values by their id (index) to an array accumulator:
const input = [
{id: 3, value: 2},
{id: 0, value: 3},
{id: 2, value: 8},
{id: 1, value: 5},
{id: 0, value: 2},
{id: 1, value: 6}
]
const result = input.reduce((r, { id, value }) => {
r[id] = +r[id] > value ? r[id] : value;
return r;
}, []);
console.log(result);
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;
}, {})
This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 5 years ago.
have array [{id: 5}, {id: 1}, {id: 2}, {id: 0}] i wanna create sorted another array [{id: 0}, {id: 1}, {id: 2}, {id: 5}]. How i can do this with function filter?
Use the sort method of the array to sort the values
array.sort(function(a, b) {
return a.id- b.id;
});