Home > OS >  How to pick all objects of json files in js?
How to pick all objects of json files in js?

Time:11-24

I have a json file which looks like this

[
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filea",
    file2: "fileb", 
    state: "equal"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filec",
    file2: "filed",
    state: "distinct"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filee",
    file2: "filef",
    state: "equal"
},
{
    path1:"xyz4",
    path2: "xyz3",
    file1: "fileg",
    file2: "fileh",
    state: "distinct"
}
]

The problem is I should get the json object data which have the type of state:distinct which should look like this

[

{
    path1:"xyz",
    path2: "xyz2",
    file1: "filec",
    file2: "filed",
    state: "distinct"
},

{
    path1:"xyz4",
    path2: "xyz3",
    file1: "fileg",
    file2: "fileh",
    state: "distinct"
}
]

Is there any solution so that, I can get a json data like above in JavaScript?

CodePudding user response:

const data = [
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filea",
    file2: "fileb", 
    state: "equal"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filec",
    file2: "filed",
    state: "distinct"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filee",
    file2: "filef",
    state: "equal"
},
{
    path1:"xyz4",
    path2: "xyz3",
    file1: "fileg",
    file2: "fileh",
    state: "distinct"
}
]

const result = data.filter((item) => item.state === 'distinct');
console.log(result)

CodePudding user response:

First of all the data you have shared is not a json. It looks like a regular js array. Second, if you want to conver to json, you can use JSON.stringify() method, but for that too, the format you have shared or the output of the following function which is derived from that data is not gonna be a valid json.

function filterData (data, key, value) {
 return data.filter((item) => item[key] === value )
}

const filteredData = filterData(data, 'state', 'distinct')

CodePudding user response:

If you are using Lodash(https://www.npmjs.com/package/lodash) or underscore package then it is very simple and you can do it in one line.

enter code here
var data = [
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filea",
    file2: "fileb", 
    state: "equal"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filec",
    file2: "filed",
    state: "distinct"
},
{
    path1:"xyz",
    path2: "xyz2",
    file1: "filee",
    file2: "filef",
    state: "equal"
},
{
    path1:"xyz4",
    path2: "xyz3",
    file1: "fileg",
    file2: "fileh",
    state: "distinct"
}
];

var distinctStates = _.filter(data, { 'state': 'distinct'});

I hope it will help you.

CodePudding user response:

You can use filter() method of Array.

Is this what you want?

Array.prototype.filter()

const datas = [
  {
      path1:"xyz",
      path2: "xyz2",
      file1: "filea",
      file2: "fileb", 
      state: "equal"
  },
  {
      path1:"xyz",
      path2: "xyz2",
      file1: "filec",
      file2: "filed",
      state: "distinct"
  },
  {
      path1:"xyz",
      path2: "xyz2",
      file1: "filee",
      file2: "filef",
      state: "equal"
  },
  {
      path1:"xyz4",
      path2: "xyz3",
      file1: "fileg",
      file2: "fileh",
      state: "distinct"
  }
];
const distincts = datas.filter(data => data.state === 'distinct');

CodePudding user response:

Use filter() method to get the JSON object data which have the type of state:distinct.

The filter() method creates a new array filled with elements that pass a test provided by a function.

const myJson = [
  {
    path1: "xyz",
    path2: "xyz2",
    file1: "filea",
    file2: "fileb",
    state: "equal",
  },
  {
    path1: "xyz",
    path2: "xyz2",
    file1: "filec",
    file2: "filed",
    state: "distinct",
  },
  {
    path1: "xyz",
    path2: "xyz2",
    file1: "filee",
    file2: "filef",
    state: "equal",
  },
  {
    path1: "xyz4",
    path2: "xyz3",
    file1: "fileg",
    file2: "fileh",
    state: "distinct",
  },
];

const result = myJson.filter((item) => item.state === "distinct");

console.log(result);

  • Related