JavaScript Filtering a json object with array of values using jquery grep filtering json object,filtering json object in javascript,filtering json

I need to filter JSON result using jQuery grep.My JSON result look like this:

 var data = { "items":[
  {
      "Name":           "Name 1",
      "City":      "city1"
  },
  {         
      "Name":           "Name 2",
      "City":      "city2"
  },
  {
      "Name":       "Name 3",
      "City":      "cat1"
  }
]}

Filter this JSON with array of Name example:

var Name= ["Name 1","Name 2"];
Answer:1

Use jQuery.grep() to filter the items array

var data = {
  "items": [{
    "Name": "Name 1",
    "City": "city1"
  }, {
    "Name": "Name 2",
    "City": "city2"
  }, {
    "Name": "Name 3",
    "City": "cat1"
  }]
}
var name = ["Name 1", "Name 2"];

var res = $.grep(data.items, function(v) {
  return name.indexOf(v.Name) > -1;
});

document.write('<pre>' + JSON.stringify(res, 0, 3) + '</pre>');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Answer:2

If you need to get the string array from existing object array using $.grep so first use $.grep for filter the object and then use $.map for get the specific output from the result object like below code will help you.

Filter the object using $.grep

var data = {
    "items": [
  {
      "Name": "Name 1",
      "City": "city1"
  },
  {
      "Name": "Name 2",
      "City": "city2"
  },
  {
      "Name": "Name 3",
      "City": "cat1"
  }
    ]
};

var objret = $.grep(data.items, function (n, i) {
    return n.Name == 'Name 1' || n.Name == 'Name 2';
});

Now you have result object in the objret variable now convert the object result to your string array out put using $.map like :-

Get OutPut

var array = $.map(objret, function (value, index) {
    return [value.Name];
});

so in the array have your required output.

Answer:3

When you want to make a graph in d3 and you read your input from a json file (for example) the code goes pretty much like this : d3.json("data/data.json", function(error, data) { data.forEach(...

When you want to make a graph in d3 and you read your input from a json file (for example) the code goes pretty much like this : d3.json("data/data.json", function(error, data) { data.forEach(...

  1. dynamic json namespace
  2. json dynamic name

I just watched The Definitive Guide to Object-Oriented JavaScript. I will never learn how to do OOP in classical way. It mentioned the new spec (ES6). But as far as I know it is not well supported. Is ...

I just watched The Definitive Guide to Object-Oriented JavaScript. I will never learn how to do OOP in classical way. It mentioned the new spec (ES6). But as far as I know it is not well supported. Is ...

Considering the following code snippet, function outer(data1){ function inner(){ console.log(data1); } return inner; } in the following two function calls, first one works but ...

Considering the following code snippet, function outer(data1){ function inner(){ console.log(data1); } return inner; } in the following two function calls, first one works but ...

  1. closure working javascript
  2. javascript closure not working

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

Similar questions have been asked before, but I'm looking for the "opposite" solution than all the others ;-) I need a JavaScript event that fires when a user decides for an option in a select field. ...

  1. javascript make select readonly
  2. javascript make select required
  3. javascript make select option unselectable
  4. javascript make select option selected
  5. javascript make select disabled
  6. javascript make select option disabled
  7. javascript make select box readonly
  8. javascript make select option readonly
  9. javascript make select
  10. javascript make select empty
  11. javascript make select dropdown
  12. javascript make select list
  13. javascript make select element
  14. javascript make select option hidden