1

Using vanilla JavaScript (supported by the latest version of Chrome, don't worry about IE) and/or lodash/underscore but no jQuery how can I take this array:

[
  {
    "id": 1,
    "places": {
      "city": "boston"
    }
  },
  {
    "id": 2,
    "places": {
      "city": "new york"
    }
  }
]

...and remove the entire object that has a city of "boston":

[
  {
    "id": 2,
    "places": {
      "city": "new york"
    }
  }
]

Please keep in mind this array could have dozens of entries. Thank you!

http://plnkr.co/edit/JW3zd6A7OcmihM4CTh1D?p=preview

0

1 Answer 1

4

One of the ways you can do this is by using filter. For example:

var dataWithoutBoston = data.filter(function (el) {
  return el.places.city !== "boston";
});

And to make it reusable, you can have a function like this:

function removeFromCity(data, name) {
  var result = data.filter(function (el) {
    return el.places.city !== name;
  });
  return result;
};
Sign up to request clarification or add additional context in comments.

1 Comment

wrap your function and you can set the city to select as argument

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.