基于数组中的一个键值如何找到另一个键的值?

时间:2019-01-04 14:18:46

标签: javascript

Reponse format

payloadbytes

我正在根据列名键在屏幕快照中共享数据,我需要找到data_type

2 个答案:

答案 0 :(得分:1)

您可以使用Array#find查找对象(或使用默认的空对象),然后访问所需的属性。

type = (array.find(el => el.column_name === e.target.value) || {}).data_type;

答案 1 :(得分:0)

Array.prototype.filter必须返回一个布尔值,以在新数组中包含或不包含该记录;

如果需要全新的阵列,请考虑使用Array.prototype.mapArray.prototype.reduce

UIImage

如果您只需要一个值,则可以使用Array.prototype.find

const newData = groups.reduce((acc, el) => {
  if (el.column_name === e.target.value) {
    acc.push(el.data_type);
  }
  return acc;
}, []);