Home > database >  Filter object by name with JavaScript
Filter object by name with JavaScript

Time:11-01

I made a request to an endpoint and I get this object, I'm filtering the name like this:

fetch('http://endpoint', requestOptions)
.then((response) => response.json())
.then((result) => {
  const onlineUsers = result.resource.items[1].onlineUsers >= 1;
  console.log(onlineUsers);
})
.catch((error) => console.log('error', error));

This workers, but I just need the result of what is in the key named Forms, but there is a possibility that it will change its position, so the items[1] it may not work anymore

This is an example of the object I receive:

{
  "type": "application/vn json",
  "resource": {
    "total": 4,
    "itemType": "application",
    "items": [
      {
        "name": "Test",
        "onlineUsers": 1
      },
      {
        "name": "Forms",
        "onlineUsers": 1
      },
      {
        "name": "Users",
        "onlineUsers": 7
      },
      {
        "name": "OnlineUsers",
        "onlineUsers": 5
      }
    ]
  },
  "method": "get",
  "status": "success"
}

Is there any way to receive this object and filter by name? Like:

if (hasName === "Forms", get onlineUsers) { 
  // Do something
}

Thanks!

CodePudding user response:

As suggested in your question, you can use filter on your array. Something like that:

console.log(
  result.resource.items.filter((item) => item.name === "Forms")
);

This will print an array with all items having the name Forms. Using your example:

[
  {
    "name": "Forms",
    "onlineUsers": 1
  }
]

If there is only one item with the name Forms, or if you want only the first one, find may be a good alternative with a similar syntax:

console.log(
  result.resource.items.find((item) => item.name === "Forms")
);

This will only print the first found object (or null if none is matching), without the array "pollution":

{
  "name": "Forms",
  "onlineUsers": 1
}

CodePudding user response:

result.resource.items.filter((item) => item.name === "Forms")

Will filter your objects for you Note, you will get back an array of all the filtered objects meeting the condition.

CodePudding user response:

If you know there's only one object that matches that name you can use find which will return the first match so you don't necessarily need to iterate over the entire array.

Here's a little general function that might help. Pass in the data, the value of name, and the property you want the value of. If there is no match it returns 'No data'.

const data={type:"application/vn json",resource:{total:4,itemType:"application",items:[{name:"Test",onlineUsers:1},{name:"Forms",onlineUsers:1},{name:"Users",onlineUsers:7},{name:"OnlineUsers",onlineUsers:5}]},method:"get",status:"success"};

function finder(data, name, prop) {
  return data.resource.items.find(item => {
    return item.name === name;
  })?.[prop] || 'No data';
}

console.log(finder(data, 'Forms', 'onlineUsers'));
console.log(finder(data, 'morf', 'onlineUsers'));
console.log(finder(data, 'Users', 'onlineUsers'));
console.log(finder(data, 'Users', 'onlineUser'));

  • Related