0

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?

Neetesshhr
  • 526
  • 1
  • 8
  • 20
  • Does this answer your question? [How to filter object array based on attributes?](https://stackoverflow.com/questions/2722159/how-to-filter-object-array-based-on-attributes) – Zac Anger Nov 24 '22 at 05:52
  • there is no JSON in your question, it's just js objects. JSON is just text that follows a strict syntax. This is not the case here. – Mister Jojo Nov 24 '22 at 05:56

5 Answers5

2

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)
1

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')
pope_maverick
  • 902
  • 8
  • 15
0

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.

  • Why using `underscore` or `loadash` . This is a standard Array#filter call withouth the need for extra dependencies – Manos Kounelakis Nov 24 '22 at 06:28
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Albert Logic Einstein Nov 27 '22 at 15:00
0

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');
ChanHyeok-Im
  • 541
  • 3
  • 11
0

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);