【问题标题】:Easy way to check to see if value exists in a object array?检查对象数组中是否存在值的简单方法?
【发布时间】:2020-01-13 06:38:20
【问题描述】:

如何检查嵌套对象中存在的值?

假设我有这样的数据:

const data = [
  {
    title: 'New Posts',
    data: [
      {
        username: 'firstnamefromnewpost',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets',
      },
      {
        username: 'another name',
        content:
          'lost content goals lets start with some',
      },
    ],
  },
  {
    title: 'Different Posts',
    data: [
      {
        username: 'usernametag',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets start',
      },
    ],
  },
];

我希望能够找到firstnamefromnewpost 是否存在于这个数组中。有没有简单的方法?

【问题讨论】:

  • 如何存在?仅在嵌入的data 对象数组的username 属性中?还是存在于任何对象的任何属性中?
  • @jfriend00 好问题。我只想获取嵌入数据的用户名属性,但也希望您对任何对象的任何属性进行解释,这将是一次很好的学习体验!
  • 您已经接受了答案,显然不需要任何财产。那将是更多的工作。

标签: javascript arrays object ecmascript-6


【解决方案1】:

简单吗?

也许你可以在字符串化数组上使用includes()

const data = [
  {
    title: 'New Posts',
    data: [
      {
        username: 'firstnamefromnewpost',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets',
      },
      {
        username: 'another name',
        content:
          'lost content goals lets start with some',
      },
    ],
  },
  {
    title: 'Different Posts',
    data: [
      {
        username: 'usernametag',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets start',
      },
    ],
  },
];

var isExists = JSON.stringify(data).includes('firstnamefromnewpost');
console.log(isExists);

或:如果您想检查嵌套 data 数组中的 username 属性:

const data = [
  {
    title: 'New Posts',
    data: [
      {
        username: 'firstnamefromnewpost',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets',
      },
      {
        username: 'another name',
        content:
          'lost content goals lets start with some',
      },
    ],
  },
  {
    title: 'Different Posts',
    data: [
      {
        username: 'usernametag',
        content: 'this is some content that will go on forever and ever',
      },
      {
        username: 'name',
        content:
          'lost content goals lets start',
      },
    ],
  },
];

function isExists(data, userName){
  for(const i of data){
    if(i.data.map(n => n.username).includes(userName)) return true
  }
  return false;
}
console.log(isExists(data, 'firstnamefromnewpost'));

【讨论】:

    猜你喜欢
    • 2017-04-27
    • 2020-01-25
    • 2012-01-04
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-01
    相关资源
    最近更新 更多