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.
Related
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 1 year ago.
i wanted represent first two letters of phone number,i want to get phone numbers first two characters. what i have tried is below Get first two of attribute and find target based on it.
let object = [
"key": {
login-attempt: 1
name: "ADMIN"
phone: "0777123456"
}]
what i have tried is below
object[0].substr(0,2);
let test = {
key: {
"login-attempt": 1,
name: "ADMIN",
phone: "0777123456",
}
};
console.log(test.key.phone.substr(0,2));
You're more likely to have an object like this:
let object = {
login-attempt: 1
name: "ADMIN"
phone: "0777123456"
};
In this case, your code might be:
let prefix = object.phone.substr(0,2);
Or you might have an array of objects:
let object = [
{
login-attempt: 1
name: "ADMIN"
phone: "0777123456"
},
{
login-attempt: 1
name: "CLERK"
phone: "2222222222"
}
];
You can get the 1st two characters of the second element like this:
let prefix = object[1].phone.substr(0,2);
If the element was nested within "key":
let object = {
key: {
login-attempt: 1
name: "ADMIN"
phone: "0777123456"
}
};
let prefix = object.key.phone.substr(0,2);
And so on...
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
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
Object {Results:Array[3]}
Results:Array[3]
[0-2]
0:Object
id=1
name: "Rick"
upper:"0.67"
1:Object
id=2
name:'david'
upper:"0.46"
2:Object
id=3
name:'ashley'
upper:null
I have this array of objects as shown above. and a variable named delete_id
delete_id = 1,2
So this indicates objects with id 1 and 2. It should delete the objects in the array of objects and give the final result as follows:
Object {Results:Array[1]}
Results:Array[3]
[0]
0:Object
id=3
name:'ashley'
upper:null
Can someone let me know how to achieve this. I tried to use this below function. It only deletes the first value in variale delete_id. i.e. id with 1 is deleted. similary if we have delete_id = 2,3 then it only deletes 2. I want to delete 2 and 3 both...
function removeID(delete_id) {
tabledata = tabledata.filter(function (obj) {
return delete_id.indexOf(obj.id);
});
You can use .split() and .map() to transform your delete_id string into an array of numeric IDs. Then, you can use .filter() to do the cleanup.
var players = [
{
id: 1,
name: "Rick",
upper: "0.67"
},{
id: 2,
name: "david",
upper: "0.46"
},{
id: 3,
name: "ashley",
upper: null
}
];
var delete_id = "1,2";
var exclude = delete_id.split(',').map(Number);
players = players.filter(function(player) {
return exclude.indexOf(player.id) == -1;
});
console.log(players);
function findObj(array,value,key) {
var result = array.filter(function (obj) {
if (obj[key] === value || obj[key] == value)
return obj;
});
return result;
};
First find the object from the
array(tabledata),value=1(delete_id),key=the key in json(id)
var selectedObj=findObj(tabledata,delete_id,'id');
get index of that object in the array
var index=tabledata.indexOf(selectedObj[0]);
delete its index
tabledata.splice(index,1);
The code runs if you change the removeID code to see if the index is equal to -1
function removeID(delete_id) {
tabledata = tabledata.filter(function(obj) {
return delete_id.indexOf(obj.id)===-1; //added === -1 here
});
}
var tabledata = [{
id: 1,
name: "Rick",
upper: "0.67"
}, {
id: 2,
name: 'david',
upper: "0.46"
}, {
id: 3,
name: 'ashley',
upper: null
}];
var ids = [1,2]; //defined it as an array (not sure if you did)
removeID(ids);
console.log(tabledata);
I assume that delete_id is an integer array. In that case you can filter to exclude provided ids with code below.
It'll check if obj.id is not in delete_id, then obj will be included in a new filtered array. Also it will leave tabledata intact.
var filtered = tabledata.filter(function(obj) {
return !(obj.id in delete_id)
})
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.