【问题标题】:Identify unique objects in array based on a specific value根据特定值识别数组中的唯一对象
【发布时间】:2020-06-23 01:15:29
【问题描述】:

我有以下对象数组,我需要根据键 img1 从该数组中识别唯一对象。我能够识别与键 img1 关联的唯一值,但不能识别键 img2 的关联值。

我目前拥有的代码,

const imgs_arr = [
    ...new Set(
      input_arr.map(item => {img_1: item.img1[0]})
    )
  ];
  return imgs_arr;

输入数组:

[{img1: ['/path/to/img1'], img2: ['/path/to/img2']},
{img1: ['/path/to/img1'], img2: ['/path/to/img3']},
{img1: ['/path/to/img1'], img2: ['/path/to/img4']},
{img1: ['/path/to/img12'], img2: ['/path/to/img5']},
{img1: ['/path/to/img12'], img2: ['/path/to/img46']},
{img1: ['/path/to/img12'], img2: ['/path/to/img45']},
{img1: ['/path/to/img12'], img2: ['/path/to/img478']}]

预期输出数组:

[{img1: '/path/to/img1', img2: '/path/to/img2'},
{img1: '/path/to/img12', img2: '/path/to/img5'}]

根据 cmets 中的问题为问题添加更多颜色。 img1 键有值,我需要从中找到唯一值,然后从第一个匹配项中找到键 img2 的对应值。

非常感谢您的帮助!

【问题讨论】:

  • 产生 path/to/img2 和 path/to/img5 作为 img2 属性值的模式是什么?
  • 我需要提取键img2的第一个值匹配键img1的第一个实例

标签: javascript arrays vue.js ecmascript-6 vuejs2


【解决方案1】:

使用 forEach 循环并使用 unqiue 键构建任何对象。从构建对象中获取Object.values

const data = [
  { img1: ["/path/to/img1"], img2: ["/path/to/img2"] },
  { img1: ["/path/to/img1"], img2: ["/path/to/img3"] },
  { img1: ["/path/to/img1"], img2: ["/path/to/img4"] },
  { img1: ["/path/to/img12"], img2: ["/path/to/img5"] },
  { img1: ["/path/to/img12"], img2: ["/path/to/img46"] },
  { img1: ["/path/to/img12"], img2: ["/path/to/img45"] },
  { img1: ["/path/to/img12"], img2: ["/path/to/img478"] }
];

const update = data => {
  const res = {};

  data.forEach(item => {
    const u_key = item.img1[0];
    if (!(u_key in res)) {
        res[u_key] = item;
    }
  });
  return Object.values(res);
};

console.log(update(data));

【讨论】:

    【解决方案2】:

    function filterArrayByImg1(arr) {
    
      let x = [];
    
      return arr.filter((e, a) => {
    
          if (!e.img1 || !e.img1[0] || x.includes(e.img1[0]))
            return false;
          else {
            x.push(e.img1[0]);
            return true;
          }
        })
        .map(e => ({
          img1: e.img1[0],
          img2: e.img2[0]
        }));
    }
    
    
    let inputArray = [{
        img1: ['/path/to/img1'],
        img2: ['/path/to/img2']
      },
      {
        img1: ['/path/to/img1'],
        img2: ['/path/to/img3']
      },
      {
        img1: ['/path/to/img1'],
        img2: ['/path/to/img4']
      },
      {
        img1: ['/path/to/img12'],
        img2: ['/path/to/img5']
      },
      {
        img1: ['/path/to/img12'],
        img2: ['/path/to/img46']
      },
      {
        img1: ['/path/to/img12'],
        img2: ['/path/to/img45']
      },
      {
        img1: ['/path/to/img12'],
        img2: ['/path/to/img478']
      }
    ];
    
    
    
    //filter the array
    let filteredArr = filterArrayByImg1(inputArray);
    
    console.log(filteredArr);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-24
      • 1970-01-01
      • 2021-12-29
      • 2018-06-18
      • 2021-10-03
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多