How to delete Object from deep javascript array? [duplicate] - javascript

This question already has answers here:
How can I remove a specific item from an array in JavaScript?
(142 answers)
Closed 6 years ago.
I want to delete object from my array posted below but nothing works. If I delete the properties it just goes to undefined. I have a recursive function that returns to me the right object. I wish to delete the second object and his children.
var data = {
parameters: [
{
id: 0,
name: "First",
value: "1",
children: []
},
{
id: 1,
name: "Second",
value: "2",
children: [
{
id: 2,
name: "Third",
value: "3",
children: []
}
]
}
],
index: 3
}

You could use Array#splice.
The splice() method changes the content of an array by removing existing elements and/or adding new elements.
data.parameters.splice(1, 1)
// index // \\ length

Related

Insert object into dynamic key nested array [duplicate]

This question already has answers here:
Iterate through object properties
(31 answers)
Add property to an array of objects
(7 answers)
Closed 11 months ago.
Desired result: Insert an object into each dynamicItem and have the full obj object.
const obj = {
item1: {},
item2: {
dynamicItem1: [{ id: 'one' }, ], // add another prop here {type: 'added'}
dynamicItem2: [{ id: 'one' }, ] // add another prop here {type: 'added'}
},
}
My attempts: I managed to insert the object, but i can't think how to rebuild the full object and it also feels like i'm doing it wrong already.
const result = Object.keys(obj.item2).map(key => {
return obj.item2[key].map(item => ({ ...item, type: 'added' }))
})

Order an array of objects by whether property exists [duplicate]

This question already has answers here:
Sorting an array of objects by property values
(35 answers)
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I have an array of teachers, where each one have a property user with image property, that can contain a string or null value.
How can i order the array, considering the user.image property?
Here is an example of structure.
const teachers = [
{
id: 1,
user: {
id: 10,
image: "image.jgp"
}
},
{
id: 3,
user: {
id: 30,
image: null
}
},
{
id: 2,
user: {
id: 20,
image: "image.jgp"
}
},
{
id: 4,
user: {
id: 40,
image: null
}
},
]
You can use Array.sort and check whether the image property is null:
const teachers=[{id:1,user:{id:10,image:"image.jgp"}},{id:3,user:{id:30,image:null}},{id:2,user:{id:20,image:"image.jgp"}},{id:4,user:{id:40,image:null}}];
const sorted = teachers.sort((a, b) => b.user.image === null ? -1 : 1)
console.log(sorted)

Check same occurrence of value in Array of Objects, then add another array in first found object

I have an array of objects, for example
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123"
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
},
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
Actual data is more than this. I've simplified the array.
Here, id is the key which can be same in more than 1 array of objects, for example in 1st and 3rd id is same.
I want to check if more than 1 objects contain the same value (id). If yes, add another array (sameIdArray) in the first object where id is same (1st in this case) and this array will now contain all those objects where that same value (id) was found and remove them from the actual array. The final array structure will be something like this.
arr = [
{
date: "2020-03-20T11:40:07.620Z",
name: "whatever",
id: "abc123",
sameIdArray: [
{
date: "2020-03-22T11:54:07.620Z",
name: "whatever2",
id: "abc123"
}
]
},
{
date: "2020-03-21T11:21:07.620Z",
name: "whatever1",
id: "def455"
}
]
You can use the groupBy functionality. You can group your data by id and use it accordingly.
You can use libraries like underscore or lodash, if using JavaScript

Delete last object from the array of objects. [duplicate]

This question already has answers here:
Remove last item from array
(28 answers)
Closed 6 years ago.
I have this array of objects and i would like to delete the last object. i.e. 2 from the list. Can someone please let me know to do this.
Object {Results:Array[3]}
Results:Array[3]
[0-2]
0:Object
id=1
name: "Rick"
Value: "34343"
1:Object
id=2
name:'david'
Value: "2332"
2:Object
id=3
name: 'Rio'
Value: "2333"
Try using the .pop() method. It'll delete the last item of an array.
obj.Results.pop();
You could just splice out the last element in the array:
obj.Results.splice(-1);
var obj = {
Results: [{
id: 1,
name: "Rick",
Value: "34343"
}, {
id:2,
name: 'david',
Value: "2332",
}, {
id: 3,
name: 'Rio',
Value: "2333"
}]
};
obj.Results.splice(-1);
console.log(obj);
You can use Array.prototype.splice to remove the last item.
var data = {
Results : [ {
id : 1,
name : "Rick",
Value : "34343"
}, {
id : 2,
name :'david',
Value : "2332"
}, {
id : 3,
name : 'Rio',
Value : "2333"
}]
};
var removed = data.Results.splice(-1,1);
document.body.innerHTML = '<pre>'+ JSON.stringify(data, null, 4) +'</pre>'
You should use array.pop() for it, it removes the last element and returns it.
You can pop() the last item out of the array.
obj.Results.pop()
For more on array methods, visit this.

Javascript splicing an array just replaces the value instead of removing it [duplicate]

This question already has answers here:
Javascript oddness with array of objects and indexOf
(2 answers)
Closed 6 years ago.
I have a menu containing a list of dishes. i want to be able to remove the dishes from the menu, i approached this by splicing the index of the dish id in the menu array.
But instead of removing the value and shortening the array, it just replaces the value by the last value in the array.
In a menu with 4 dishes, after removing 3 of them, the array still contains 4 values, all of them are the same.
$scope.fjernRet = function(ret, menu) {
//console.log(ret._id)
var index = menu.retter.indexOf(ret._id);
if (index > -1) {
menu.retter.splice(index, 1)
}
menuService.update(menu);
socket.syncUpdates('menu', $scope.menuer);
}
menu.retter could look like
[{
_id: '56e827ba0ec7a8d02bf7747d',
name: 'test',
info: 'testinfo',
type: 'kød',
active: true
}, {
_id: '56e827ba0ec7a8d02bf77473',
name: 'test3',
info: 'testinfo3',
type: 'kød',
active: true
}, {
_id: '56e827ba0ec7a8d02bf77474',
name: 'test4',
info: 'testinfo4',
type: 'salat',
active: false
}];
Replace this line:
var index = menu.retter.indexOf(ret._id);
by this:
var index = menu.retter.map(function(x){
return x._id
}).indexOf(ret._id);
The Array.map() will return a mapped array with only the _id's, then your .indexOf() can work.
Hope it helps.

Categories