【问题标题】:I am trying to retrieve result from 2 array我正在尝试从 2 个数组中检索结果
【发布时间】:2020-07-10 09:49:01
【问题描述】:

我正在尝试根据第二个数组文本过滤第一个数组,但我不知道如何将计数过滤器从第二个数组更新为第一个数组。

第一个数组:

var firstArray =[{text:"India",count:1,checked:true},
{text:"America",count:2,checked:false},
{text:"Uk",count:1,checked:true}];

第二个数组:

var secondArray=[
{text:"India",count:1,checked:false},
{text:"America",count:1,checked:false},
{text:"Uk",count:1,checked:false}
];

代码:

var result=firstArray.filter(o1 => secondArray.some(o2 => o1.text === o2.text));

我目前得到的结果:

var result=[
{text:"India",count:1,checked:true},
{text:"America",count:2,checked:false},
{text:"Uk",count:1,checked:true}
];

我想要得到的结果如下:

var result=[
{text:"India",count:1,checked:true},
{text:"America",count:1,checked:false},
{text:"Uk",count:1,checked:true}
];

谢谢你的帮助!!!

【问题讨论】:

标签: javascript arrays reactjs filter


【解决方案1】:

您可以像这样映射第一个数组并从第二个数组中选择计数

var firstArray = [
  { text: "India", count: 1, checked: true },
  { text: "America", count: 2, checked: false },
  { text: "Uk", count: 1, checked: true },
];

var secondArray = [
  { text: "India", count: 1, checked: false },
  { text: "America", count: 1, checked: false },
  { text: "Uk", count: 1, checked: false },
];

firstArray
  .map((x) => {
    const tmp = secondArray.find((y) => y.text === x.text);
    return tmp ? { ...x, count: tmp.count } : null
})
  .filter(Boolean);

【讨论】:

    【解决方案2】:

    可能的解决方案如下

    var firstArray = [{
        text: "India",
        count: 1,
        checked: true
      },
      {
        text: "America",
        count: 2,
        checked: false
      },
      {
        text: "Uk",
        count: 1,
        checked: true
      }
    ];
    
    var secondArray = [{
        text: "India",
        count: 1,
        checked: false
      },
      {
        text: "America",
        count: 1,
        checked: false
      },
      {
        text: "Uk",
        count: 1,
        checked: false
      }
    ];
    
    var result = firstArray.map(o1 => {
      const o2 = secondArray.find(o2 => o1.text === o2.text);
      if (!o2) return;
      return { ...o1,
        count: o2.count
      }
    }).filter(o1 => !!o1);
    
    console.log(result)

    【讨论】:

      【解决方案3】:

      此解决方案将合并两个数组以获得所需的结果(从答案中推断)

      var firstArray =[{text:"India",count:1,checked:true},
      {text:"America",count:2,checked:false},
      {text:"Uk",count:1,checked:true}];
      
      var secondArray=[
      {text:"India",count:1,checked:false},
      {text:"America",count:1,checked:false},
      {text:"Uk",count:1,checked:false}
      ];
      
      const newArray = firstArray.map((el1) => {
        let sameElList = secondArray.filter((el2) => el2.text === el1.text);
        let sameEl = sameElList.length > 0 && sameElList.pop();
        if(!sameEl){
          return el1;
        }
        el1.count = Math.min(el1.count, sameEl.count);
        el1.checked = el1.checked || sameEl.checked;
        return el1;
       });
       
       console.log(newArray);
          

      【讨论】:

        猜你喜欢
        • 2017-03-27
        • 2022-01-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多