0

i want to make a search in a json file by query Example: My json file:

[{
"Id":1,
"name":"test"
},
{
"Id":2,
"name":"test"
}]

And if i search {"name":"test"} i get the full objects whos name is test

ADD: I misspoke its the user who enter the query how can i get the key and the value from the query {key:value} and the user can entre multiple keys and values (like the search in atlas mongodb) UP!!

Nassim
  • 25
  • 7
  • Duplicate: https://stackoverflow.com/questions/13964155/get-javascript-object-from-array-of-objects-by-value-of-property – Quentin Apr 20 '22 at 09:57

2 Answers2

3

As per your question you are using JSON inside of an array. So you can simply filter the array to find JSON which contains name="test"

Follow the below step:

let userArray = [{"Id":1,"name":"test"},{"Id":2,"name":"test"}]

const filteredUser = userArray.filter((user) => user.name === "test")
Mihir Shah
  • 518
  • 2
  • 9
  • I misspoke its the user who enter the query how can i get the key and the value from the query {key:value} and the user can entre multiple keys and values (like the search in atlas mongodb) – Nassim Apr 20 '22 at 11:52
0

Try something simple:

var myjson ='[{"Id":1,"name":"test"},{"Id":2,"name":"test"},{"Id":3,"name":"nottest"}]';
var slag = JSON.parse(myjson).filter(doc => doc.name === 'test');
console.log(slag);
D A
  • 1,724
  • 1
  • 8
  • 19