-1
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);
    });
Patrick
  • 3,938
  • 6
  • 20
  • 32

4 Answers4

1
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);
misss-popcorn
  • 591
  • 2
  • 12
1

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);
Arnauld
  • 5,847
  • 2
  • 15
  • 32
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);
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

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)
})
Dulmandakh
  • 21
  • 2