-1

I have following JSON

[  
   {  
      "Id":1068,
      "Name":"jone",
      "SortOrder":0
   },
   {  
      "Id":1077,
      "Name":"John ",
      "SortOrder":0
   },
   {  
      "Id":1074,
      "Name":"sasasa",
      "SortOrder":0
   },
   {  
      "Id":1078,
      "Name":"fdfdfd",
      "SortOrder":0
   },
   {  
      "Id":1029,
      "Name":"fdfdfd",
      "SortOrder":0
   },
   {  
      "Id":1076,
      "Name":"sdf",
      "SortOrder":0
   },
   {  
      "Id":1030,
      "Name":"erer",
      "SortOrder":0
   },
   {  
      "Id":11,
      "Name":"tryryr",
      "SortOrder":0
   },
   {  
      "Id":1008,
      "Name":"bvbf",
      "SortOrder":0
   },
   {  
      "Id":9,
      "Name":"jkk",
      "SortOrder":0
   },
   {  
      "Id":19,
      "Name":"pioku",
      "SortOrder":0
   },
   {  
      "Id":5,
      "Name":"cssxs",
      "SortOrder":0
   },
   {  
      "Id":1009,
      "Name":"mhmh",
      "SortOrder":0
   }
]

Now I need to remove the Item from JSON by passing IDs. AS an example when I pass 1068,1077 ids(comma seperated) to the function I need to remove related data from the JSON.

[ { "Id":1074, "Name":"sasasa", "SortOrder":0 }, { "Id":1078, "Name":"fdfdfd", "SortOrder":0 }, { "Id":1029, "Name":"fdfdfd", "SortOrder":0 }, { "Id":1076, "Name":"sdf", "SortOrder":0 }, { "Id":1030, "Name":"erer", "SortOrder":0 }, { "Id":11, "Name":"tryryr", "SortOrder":0 }, { "Id":1008, "Name":"bvbf", "SortOrder":0 }, { "Id":9, "Name":"jkk", "SortOrder":0 }, { "Id":19, "Name":"pioku", "SortOrder":0 }, { "Id":5, "Name":"cssxs", "SortOrder":0 }, { "Id":1009, "Name":"mhmh", "SortOrder":0 } ]

When as Pass 1009,1068,1074,1078,1029,1076,1030,11,1008 to the function following JSON sholud be the result.

[  
   {  
      "Id":9,
      "Name":"jkk",
      "SortOrder":0
   },
   {  
      "Id":19,
      "Name":"pioku",
      "SortOrder":0
   },
   {  
      "Id":5,
      "Name":"cssxs",
      "SortOrder":0
   },
   {  
      "Id":1009,
      "Name":"mhmh",
      "SortOrder":0
   }
]

What is the most efficient way to do this? And please be noted. I cannot use ES6 or ES5, because, some of the ES5,ES6 functions not working in the IE and Safari browsers.

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • use `filter` and `includes` – Code Maniac Jul 31 '19 at 17:25
  • 1
    Unless the objects are stored by keys equal to their id, you're gonna have to iterate through the whole JSON to find the right objects. I'd suggest iterating once through the whole structure just to restructure it as objects stored by their Id. Then you have O(1) look up. – Dylan Landry Jul 31 '19 at 17:26
  • @DylanLandry Could you please give me a sample code to that, So then I can easily understand what u say.pls – Sachith Wickramaarachchi Jul 31 '19 at 17:31
  • @CodeManiac Can u please give me a sample code.pls – Sachith Wickramaarachchi Jul 31 '19 at 17:37
  • 1
    @Adam please provide what you have tried. Both solutions have documentation on the web. for instance: https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects – scrappedcola Jul 31 '19 at 17:56
  • @DylanLandry is the correct way to go about this, as otherwise you are looking at quadratic time solutions (which get ludicrously slow as data grows). – Cal Irvine Jul 31 '19 at 18:16

2 Answers2

4

This does the trick. Could be made less verbose with an Array method like .map or .filter.

var myFilter = [1009,1068,1074,1078,1029,1076,1030,11,1008];
var myData = [
  { "Id":1074, "Name":"sasasa", "SortOrder":0 },
  { "Id":9, "Name":"jkk", "SortOrder":0 },
  { "Id":19, "Name":"pioku", "SortOrder":0 },
  { "Id":5, "Name":"cssxs", "SortOrder":0 },
  { "Id":1009, "Name":"mhmh", "SortOrder":0 }
];
function filterOut(data, filter){
  result = [];
  for (let i = 0; i < data.length; i++){
    if(filter.indexOf(parseInt(data[i].Id)) == -1){
      result.push(data[i]);
    }
  }
  return(result);
}

var output = filterOut(myData, myFilter);
console.log(output);

(Note that your expected result was incorrect as it contains 1009, which is one of the Id values you asked to filter out. I removed some of the data objects from your list for brevity.)

EDIT: I just noticed your 'no modern JS' requirement. I think I've removed anything from ES5 onward.

Cat
  • 4,141
  • 2
  • 10
  • 18
2

My suggestion would be to write the simplest workable code and then polyfill or transpile to support your targeted environments.

With that in mind, here is a simple version written in modern style:

const removeByIds = (ids, idNbrs = ids.split(',').map(Number)) => 
  (data) => data.filter(({Id}) => !idNbrs.includes(Id))

const data = [{"Id": 1077, "Name": "John ", "SortOrder": 0}, {"Id": 9, "Name": "jkk", "SortOrder": 0}, {"Id": 19, "Name": "pioku", "SortOrder": 0}, {"Id": 5, "Name": "cssxs", "SortOrder": 0}]

console .log (
  removeByIds ('1009,1068,1074,1078,1029,1076,1030,11,1008') (data)
)
Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103