JavaScript How to merge two arrays and sum values of duplicate objects using lodash merge arrays values,merge arrays values php,javascript merge arrays

There are two arrays:

[
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 5},
  {"id": "342343343t4t34rwefewfd53", "quantity": 3}
]

and

[
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 2},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 1}
]

How to combine them into one summing quantity?

[
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 3},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 6},
  {"id": "342343343t4t34rwefewfd53", "quantity": 3}
]

One of them can be empty sometimes

Answer:1

You can do it with plain JavaScript.

Use Array.reduce() to make an intermediate dictionary by id and accumulate the quantities, then turn it into an array with Object.values():

const arr1 = [
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 5},
  {"id": "342343343t4t34rwefewfd53", "quantity": 3}
];
const arr2 = [
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 2},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 1}
];

const result = Object.values([...arr1, ...arr2].reduce((acc, { id, quantity }) => {
  acc[id] = { id, quantity: (acc[id] ? acc[id].quantity : 0) + quantity  };
  return acc;
}, {}));

console.log(result);
Answer:2

You can use lodash but modern vanilla JS is just as viable and performant. I would imagine the other answers will be using functional methods like reduce, so here's a version that uses a simple for/of loop with find rather than a dictionary lookup which might be longer, but it is a little easier to follow.

const arr1 = [{"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1}, {"id": "344430b94t4t34rwefewfdff", "quantity": 5}, {"id": "342343343t4t34rwefewfd53", "quantity": 3}];
const arr2 = [{"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 2}, {"id": "344430b94t4t34rwefewfdff", "quantity": 1}];

function merge(arr1, arr2) {

  // Merge the arrays, and set up an output array.
  const merged = [...arr1, ...arr2];
  const out = [];

  // Loop over the merged array
  for (let obj of merged) {

    // Destructure the object in the current iteration to get
    // its id and quantity values
    const { id, quantity } = obj;

    // Find the object in out that has the same id
    const found = out.find(obj => obj.id === id);

    // If an object *is* found add this object's quantity to it...
    if (found) {
      found.quantity += quantity;

    // ...otherwise push a copy of the object to out
    } else {
      out.push({ ...obj });
    }
  }
  
  return out;

}

console.log(merge(arr1, arr2));
Answer:3

You can just do this with reduce:

let a1 = [
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 2},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 1}
];

let a2 = [
  {"id": "5c5030b9a1ccb11fe8c321f4", "quantity": 1},
  {"id": "344430b94t4t34rwefewfdff", "quantity": 5},
  {"id": "342343343t4t34rwefewfd53", "quantity": 3}
];

let result = Object.values(a1.concat(a2).reduce((acc, v) => {
   if (!acc[v.id]) {
       acc[v.id] = {id: v.id, quantity: 0};
   }
   acc[v.id].quantity += v.quantity;
   return acc;
}, {}));

console.log("Results: ", result);
Answer:4

I need to distinguish between Internet Explorer's builds 11.0.9600.XXXX and 11.0.10240.XXXX in my website. I tried simple JavaScript/Angular/JQuery plugins but they only give the major (11) and minor ...

I need to distinguish between Internet Explorer's builds 11.0.9600.XXXX and 11.0.10240.XXXX in my website. I tried simple JavaScript/Angular/JQuery plugins but they only give the major (11) and minor ...

I have an array like the following: data = [{'province': 'North', 'level': 1, 'count': 204}, {'province': 'South', 'level': 1, 'count': 24}, {'province': 'East', 'level': 1, 'count': 24}, ...

I have an array like the following: data = [{'province': 'North', 'level': 1, 'count': 204}, {'province': 'South', 'level': 1, 'count': 24}, {'province': 'East', 'level': 1, 'count': 24}, ...

  1. remove keys from dictionary python
  2. remove keys from keyboard
  3. remove keys from object javascript
  4. remove keys from macbook pro
  5. remove keys from array php
  6. remove keys from mechanical keyboard
  7. remove keys from laptop keyboard
  8. remove keys from ssh-agent
  9. remove keys from apple keyboard
  10. remove keys from keyboard to clean
  11. remove keys from macbook air
  12. remove keys from chromebook
  13. remove keys from hash ruby
  14. remove keys from dict python
  15. remove keys from json object
  16. remove keys from mac keyboard
  17. remove keys from dell keyboard
  18. remove keys from dict
  19. remove keys from redis
  20. remove keys from lenovo laptop

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...

So I'm creating a basic website, and I used the typewriter function from w3schools: https://www.w3schools.com/howto/howto_js_typewriter.asp My problem is that I want to display the text on 2 lines, ...

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

When I use the document.write function to print the number in the code below, it inserts 0 to the right side of each output. Why is this happening? The code works as expected for the alert function. ...

  1. when to use document.write
  2. when would you use document.write()