本文介绍了JavaScript数组相等性控制和值更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个长度可变的数组,我尝试过如果没有想要的值,请将其从该数组中删除,如果它更改了数组2个相同的序列号,则更改总和值

Hi I have two changeable length arrays and I tried If there is no value I want, delete it from that array and change the sum value if it has changed array 2 same serials code

array1 = [
    {
      "serial": "3",
      "sum": "1"
    },
    {
      "serial": "700",
      "sum": "2"
    },
    {
      "serial": "300",
      "sum": "1"
    },
]

array2 = [{
      "someting": 10,
      "sum": "3",
      "serialList": ["700","711"],
    },

   {
      "someting": 10,
      "sum": "1",
      "serialList": ["300"],
    },
    {
      "someting": 10,
      "sum": "2",
      "serialList": [],
    }
]  

他是我的两个数组,因为我说数组的长度是可变的,有时array1的长度大,有时array2的,我想要如果array1中的序列号在array2中不存在,则从arr中删除ay1元素,如果更改了array2相同的序列号,则更改总和值,根据上述array1 [0]序列号不存在,且array1 [1]总和值不同array2 [0]总和值更改为总和array1 [1]到array2 [0],序列号300到数组的总和不执行任何操作,我想输出array1的是:

his my two array as I said arrays length is changeable sometimes array1 length big, sometimes array2 and I want If serial number in array1 does not exist in array2 delete from array1 element and change the sum value if it has changed array2 same serials code, according to above array1[0] serial codes does not exist and array1[1] sum value different array2[0] sum value change to sum value array1[1] to array2[0], serial number 300 to same sum number to array don't do anything I want to output array1 is:

array1 = [
    {
      "serial": "700",
      "sum": "3"
    },
    {
      "serial": "300",
      "sum": "1"
    },
]


推荐答案

使用

array1.flatMap(el => {
  // find array2 element with array1 element's serial
  const array2el = array2.find(({ serialList }) =>
    serialList.includes(el.serial)
  );
  if (array2el) {
    if (array2el.sum !== el.sum) {
      el.sum = array2el.sum; // sum different, update
    }
  } else {
    return []; // return [] to delete
  }
  return [el]; // return [el] to keep
});
const array1 = [
  {
    serial: "3",
    sum: "1"
  },
  {
    serial: "700",
    sum: "2"
  },
  {
    serial: "300",
    sum: "1"
  }
];

const array2 = [
  {
    someting: 10,
    sum: "3",
    serialList: ["700", "711"]
  },

  {
    someting: 10,
    sum: "1",
    serialList: ["300"]
  },
  {
    someting: 10,
    sum: "2",
    serialList: []
  }
];

const processedArray1 = array1.flatMap(el => {
  const array2el = array2.find(({ serialList }) =>
    serialList.includes(el.serial)
  );
  if (array2el) {
    if (array2el.sum !== el.sum) {
      el.sum = array2el.sum;
    }
  } else {
    return []; // delete
  }
  return [el]; // return el
});

console.log(processedArray1);

使用

const processedArray1 = array1.reduce((acc, el) => {
  // find array2 element with array1 element's serial
  const array2el = array2.find(({ serialList }) =>
    serialList.includes(el.serial)
  );
  if (array2el) {
    if (array2el.sum !== el.sum) {
      el.sum = array2el.sum; // sum different, update
    }
    acc.push(el); // push into filtered array if found in array2
  } 
  return acc;
}, []);
const array1 = [
  {
    serial: "3",
    sum: "1"
  },
  {
    serial: "700",
    sum: "2"
  },
  {
    serial: "300",
    sum: "1"
  }
];

const array2 = [
  {
    someting: 10,
    sum: "3",
    serialList: ["700", "711"]
  },

  {
    someting: 10,
    sum: "1",
    serialList: ["300"]
  },
  {
    someting: 10,
    sum: "2",
    serialList: []
  }
];

const processedArray1 = array1.reduce((acc, el) => {
  const array2el = array2.find(({ serialList }) =>
    serialList.includes(el.serial)
  );
  if (array2el) {
    if (array2el.sum !== el.sum) {
      el.sum = array2el.sum;
    }
    acc.push(el)
  } 
  return acc;
}, []);

console.log(processedArray1);

这篇关于JavaScript数组相等性控制和值更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 00:59