This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
How do I remove a property from a JavaScript object?
(37 answers)
Remove property for all objects in array
(18 answers)
How to omit specific properties from an object in JavaScript
(16 answers)
Closed 17 days ago.
I'm trying to remove specific object property of objects in array. Look at the code:
let propertyToRemove = "product"; // propertyToRemove could be anything, "cost", ...
let array = [
{product: "pen", cost: 2, color: "blue", ...}, // ... means there are other properties
{product: "pencil", cost: 1, color: "red", ...},
{product: "book", cost: 4, color: "purple", ...}
]
Expected output:
let array = [
{cost: 2, color: "blue", ...},
{cost: 1, color: "red", ...},
{cost: 4, color: "purple", ...}
]
How could it be achieved? Any support is much appreciated.
Related
This question already has answers here:
How to group by and sum an array of objects? [duplicate]
(2 answers)
Sum similar keys in an array of objects
(15 answers)
Closed 9 months ago.
let array = [{
fruit: "banana",
number: 1
},
{
fruit: "banana",
number: 2
},
{
fruit: "rambutan",
number: 1
},
{
fruit: "rambutan",
number: 1
}
]
I would first like to find the total number of one fruit, then remove the duplicates, so that the array will become:
[{
fruit: "banana",
number: 3
},
{
fruit: "rambutan",
number: 2
}
]
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());
This question already has answers here:
How can I group an array of objects by key?
(32 answers)
Most efficient method to groupby on an array of objects
(58 answers)
Group array items using object
(19 answers)
Closed 2 years ago.
Im building a function on Javascript to reduce an array like this:
var myArray = [
{sku: "one", price: "3"},
{sku: "two", price: "5"},
{sku: "one", price: "2"},
{sku: "three", price: "3"},
{sku: "three", price: "9"}
];
to this:
{ one: [ '3', '2' ], two: [ '5' ], three: [ '3', '9' ] }
in order to categorize my skus. It works just fine right now.
function itemQuantity(data) {
let group_to_values = data.reduce(function (obj, item) {
obj[item.sku] = obj[item.sku] || [];
obj[item.sku].push(item.price);
return obj;
}, {});
return group_to_values;
}
console.log(itemQuantity(myArray));
My issue comes when I try to modify it. instead of having one: [ '3', '2' ] I need the NUMBER of items (or quantity) {one: 2, two: 1...} I trieed to add obj.length but cant make it work!
Please help!
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;
}, {})