2

I have an array of objects looking similar to this

[{ id: "11", name: "Car", symbol: "CA" }
,{ id: "13", name: "Cycle", symbol: "CY" }
,{ id: "15", name: "Train", symbol: "TA" }
,{ id: "3", name: "Ufo", symbol: "UF" }]

Lets assume i have this string Car how do i search through the array of objects to find which object if any contains that particular string in the name key and then remove that object from the array of objects?

This is what i got so far (Basically nothing because i dont know what to do from here)

function remove_obj_from_array_of_objs(str, array_of_objs){

}
ii iml0sto1
  • 869
  • 10
  • 21
  • 1
    Offtopic: Better variable names would be `needle` and `haystack`, or `search` and `collection`. These are more conform other functions, and `string` is a reserved keyword, not smart to use those :) – Martijn Feb 13 '18 at 15:40
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter – Sirko Feb 13 '18 at 15:41
  • 1
    You can use the filter function of your array. `array_of_objs.filter(item => item.name !== str)`. It returns a new array without object with name == str – Okoch Feb 13 '18 at 15:41
  • https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript https://stackoverflow.com/questions/1789945/how-to-check-whether-a-string-contains-a-substring-in-javascript https://stackoverflow.com/questions/5767325/how-do-i-remove-a-particular-element-from-an-array-in-javascript – Yury Tarabanko Feb 13 '18 at 15:42

4 Answers4

7

You can use filter() and includes() methods for this.

const data = [{ id: "11", name: "Car", symbol: "CA" },{ id: "13", name: "Cycle", symbol: "CY" },{ id: "15", name: "Train", symbol: "TA" },{ id: "3", name: "Ufo", symbol: "UF" }]

const result = data.filter(({name}) => !name.includes('Car'))
console.log(result)
Nenad Vracar
  • 102,378
  • 14
  • 116
  • 136
2

Try Array's filter() like the following:

var carArr = [{ id: "11", name: "Car", symbol: "CA" }
,{ id: "13", name: "Cycle", symbol: "CY" }
,{ id: "15", name: "Train", symbol: "TA" }
,{ id: "3", name: "Ufo", symbol: "UF" }];

var notIncludesCar = carArr.filter(item => !item.name.includes("Car"));

console.log(notIncludesCar);
Mamun
  • 58,653
  • 9
  • 33
  • 46
1

Use Array.filter to filter the result of array based on some condition.

_arr.filter( el => el.name!==_str);

var data = [{ id: "11", name: "Car", symbol: "CA" }
,{ id: "13", name: "Cycle", symbol: "CY" }
,{ id: "15", name: "Train", symbol: "TA" }
,{ id: "3", name: "Ufo", symbol: "UF" }]

function remove_item_array_of_objs(_str, _arr){
  return  _arr.filter( el => el.name!==_str);
}

var resp = remove_item_array_of_objs("Car", data);
console.log(resp);
void
  • 33,471
  • 8
  • 45
  • 91
0

First you loop over your array and compare Key value with your Search String, if equals, you remove object with his index from array :

function remove_item_array_of_objs(string, array_of_objs){
    for (var i=0; i < array_of_objs.length; i++) {
        if (array_of_objs[i].name === string) {
            array_of_objs.splice(i, 1);
        }
    }
}
Léo R.
  • 3,041
  • 1
  • 8
  • 21