0

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": [],
    }
]  

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"
    },
]

1 Answer 1

1

Using a flatMap

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);

Using a reduce

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);

Sign up to request clarification or add additional context in comments.

2 Comments

It is work thank you,well how to difference with reduce and flatMap performance or something?
@Alisan26 No, not really, I just thought of the flatMap solution first and then after posting realized it could be simplified a bit. The reduce is likely a little more performant since it solves in a single pass versus 2-passes of the flatMap which is identical to map followed by flat with depth 1. flatMap has the same Big-O complexity as reduce though, O(n), so in terms of computing they are the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.