Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to merge duplicate value in array of object site:stackoverflow.com

var myArray = [
    {productId: 116605, productserialno: "324234"},
    {productId: 106290, productserialno: "12121"},
    {productId: 106290, productserialno: "12121"},
    {productId: 106293, productserialno: "4324343"}
];

var grouped = myArray.reduce(function (obj, product) {
    obj[product.productId] = obj[product.productId] || [];
    obj[product.productId].push(product.productserialno);
    return obj;
}, {});

var groups = Object.keys(grouped).map(function (key) {
    return {product: key, productserialno: grouped[key]};
});

var pre = document.createElement("pre");
pre.innerHTML = JSON.stringify(groups, null, 4);
document.body.appendChild(pre);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to use class in jsp in eclipse 
Javascript :: get random hsl value, javascript 
Javascript :: on first time this.file.readAsDataURL not working ios 
Javascript :: js event problem solving 
Javascript :: pass status of checkbox to a function react js 
Javascript :: enable clipboard 
Javascript :: enviroment variable not getting recognised in NuxtJS 
Javascript :: @webfilter objectify where we can use 
Javascript :: javascript got array object by id 
Javascript :: divcontainer is null 
Javascript :: how to auto click webpage in angular 
Javascript :: node code to read json file 
Javascript :: native base change track color 
Javascript :: getx remove all previous routes 
Javascript :: how to put the value in the fom using javascript 
Javascript :: ${product} meaning in react js 
Javascript :: Checking equality with Promise.resolve vs async return 
Javascript :: Dynamically bind layouts android with json array 
Javascript :: join-an-array-by-a-comma-and-a-space 
Javascript :: vuex get data in mounted 
Javascript :: strip add usage api docuemntation 
Javascript :: react router dom two page form 
Javascript :: Spotify analytics intergration 
Javascript :: sequelize findall in array 
Javascript :: javascript options documentation 
Javascript :: jquery input cvv format 
Javascript :: custom hook for unique items in react 
Javascript :: Upload literal unsupported graphql 
Javascript :: how to show numbers in javascript using while loop 
Javascript :: react how to sleep 1 second 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =