简体   繁体   English

如何获取此数组的特定值

[英]How to get an specific value of this array

I have this array with addresses and countries associated to each address.我有这个数组,其中包含与每个地址关联的地址和国家/地区。

So Im trying to get the createdAt value from address where the country name is USA所以我试图从国家名称为美国的地址获取 createdAt 值

How can I return exactly this ('1623775723413')我怎样才能准确地返回这个('1623775723413')

[
  address {
    id: 1,
    createdAt: '1623775723413',
    updatedAt: '1623775723413',
    description: '123 W Beaver',
    country: Country {
      id: 1,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'USA',
    }
  },
  address {
    id: 2,
    createdAt: '1623775809090',
    updatedAt: '1623775809090',
    description: '345 MH Lodge',
    country: Country {
      id: 2,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'Germany',
    }
  },
  address {
    id: 3,
    createdAt: '1623775999999',
    updatedAt: '1623775999999',
    description: '564 Brighton',
    country: Country {
      id: 3,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'France',
    }
  }
]

You have to use find method from Array prototype - it will return first matching element, or undefined if no matching elements are present:您必须使用 Array 原型中的 find 方法——它将返回第一个匹配元素,如果不存在匹配元素,则返回 undefined:

const item = arr.find(addr => addr.country.name === 'USA');

if (item) {
  console.log(item.country.createdAt);
}

Plus as mentioned in answer above, array is in wrong format.另外,如上所述,数组格式错误。

You can shorten solution even more by using optional chaining (it is widely supported by all modern browsers, except for internet explorer - so widely supported by all modern browsers):您可以通过使用可选链接来进一步缩短解决方案(它被所有现代浏览器广泛支持,除了 Internet Explorer - 所有现代浏览器都广泛支持):

console.log(arr.find(addr => addr.country.name === 'USA')?.country.createdAt);

You cant format arrays like that in JavaScript.您不能像在 JavaScript 中那样格式化 arrays。 So you first need to format it to look like this.因此,您首先需要将其格式化为如下所示。 The way you are writing them are like associative arrays in PHP but Javascript doesn't have that.您编写它们的方式就像 PHP 中的关联 arrays 但 Javascript 没有。

const items = [
  {
    id: 1,
    createdAt: '1623775723413',
    updatedAt: '1623775723413',
    description: '123 W Beaver',
    country: {
      id: 1,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'USA',
    }
  },
  {
    id: 2,
    createdAt: '1623775809090',
    updatedAt: '1623775809090',
    description: '345 MH Lodge',
    country: {
      id: 2,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'Germany',
    }
  },
  {
    id: 3,
    createdAt: '1623775999999',
    updatedAt: '1623775999999',
    description: '564 Brighton',
    country: {
      id: 3,
      createdAt: '1623556846082',
      updatedAt: '1623556846082',
      name: 'France',
    }
  }
]

You then need to loop over the array and find the array the object that you want.然后,您需要遍历数组并找到您想要的数组 object。

for (let index = 0; index < items.length; index++) {
  const singleItem = items[index];
  if(items[index].country.name === 'USA'){
    console.log(items[index].createdAt)
    break;
  }
}

Edit .编辑.

The other answer would actually be a faster way to find the country so you could combine these two answers together to fully solve the question.另一个答案实际上是查找国家/地区的更快方法,因此您可以将这两个答案结合在一起以完全解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM