-2
[
  {
    "country": "Afghanistan",
    "products": {
      "beer": 2235,
      "wine": 2274,
      "juices": 7
    }
  },
  {
    "country": "Asia",
    "products": {
      "beer": 2362,
      "wine": 20204,
      "juices": 238
    }
  },
  {
    "country": "Italy",
    "products": {
      "beer": 267,
      "wine": 459,
      "juices": 3
    }
  },
  {
    "country": "North America",
    "products": {
      "beer": 2367,
      "wine": 4359,
      "juices": 33
    }
  }
]

I have the above object and I want to remove both "Asia" and "North America", along with the whole row of data associated with them. How do I go about it?

Optional: What about if I want to remove all countries that have less than 500 beer products?

matrixguy
  • 286
  • 1
  • 6
  • 30
Artvader
  • 906
  • 2
  • 15
  • 31
  • Please visit [help], take [tour] to see what and [ask]. Do some research, search for related topics on SO; if you get stuck, post a [mcve] of your attempt, noting input and expected output. – mplungjan May 04 '20 at 11:19
  • Does this answer your question? [How do I remove a property from a JavaScript object?](https://stackoverflow.com/questions/208105/how-do-i-remove-a-property-from-a-javascript-object) – Run_Script May 04 '20 at 11:23

4 Answers4

2

You can use filter

var countries = [
    { "country": "Afghanistan", "products": { "beer": "2235", "wine": 2274, "juices": 7 } },
    { "country": "Asia", "products": { "beer": "2362", "wine": 20204, "juices": 238 } },
    { "country": "Italy", "products": { "beer": "267", "wine": 459, "juices": 3 } },
    { "country": "North America", "products": { "beer": "2367", "wine": 4359, "juices": 33 } }
]


var result = countries.filter(({country}) => country !== 'North America' && country !=='Asia' );
console.log(result)
Narendra Chouhan
  • 2,291
  • 1
  • 15
  • 24
2

You can use Array.filter() to achieve this.

var countries = [
{ "country": "Afghanistan", "products": { "beer": "2235", "wine": 2274, "juices": 7 }},
{ "country": "Asia", "products": { "beer": "2362", "wine": 20204, "juices": 238 }},
{ "country": "Italy", "products": { "beer": "267", "wine": 459, "juices": 3 }},
{ "country": "North America", "products": { "beer": "2367", "wine": 4359, "juices": 33 }}
]

// without Asia and North America
var newCountries = countries.filter(({country}) => country !== 'Asia' && country !== 'North America');

console.log(newCountries);

// with beer products more that 500
var moreBeerCountries = countries.filter(({products}) => products.beer > 500);

console.log(moreBeerCountries);
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51
2

If you want to remove countries that have less than 500 beer products

var countries = [
    { "country": "Afghanistan", "products": { "beer": "2235", "wine": 2274, "juices": 7 } },
    { "country": "Asia", "products": { "beer": "2362", "wine": 20204, "juices": 238 } },
    { "country": "Italy", "products": { "beer": "267", "wine": 459, "juices": 3 } },
    { "country": "North America", "products": { "beer": "2367", "wine": 4359, "juices": 33 } }
]
const filtered = countries.filter((country) => parseInt(country.products.beer) > 500)

console.info(filtered)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Umair Sarfraz
  • 5,284
  • 4
  • 22
  • 38
0

Use simple while loop and splice function.

var i = 0;
while (i < countries.length) {
    if(countries[i].products.beer < 500) {
        countries.splice(i, 1);
    } else {
        ++i;
    }
}
Tito
  • 663
  • 1
  • 8
  • 14