On the merging of arrays

        var a1=[
            {name:"a",value:11},
            {name:"b",value:22},
            {name:"c",value:33},
            {name:"d",value:44},
        ]
        var a2=[
            {name:"a",value:111},
            {name:"b",value:222},
            {name:"c",value:333},
            {name:"d",value:444},
        ]
        var b = [
            {name:"a",value:11},
            {name:"b",value:22},
            {name:"c",value:333},
            {name:"d",value:44},
        ]

I want to merge the two arrays A1 and a2 and store them in array b. Can we merge. The master said that there must be a mapping comparison and a control relationship before it can be merged. Because I am the front end, I don"t know much about the primary key of the database.

Apr.11,2021

are there any restrictions, such as merging different name or value, or no condition, that is, merging

if there are no conditions, you can use concat

b = b.concat(a1,a2)

restricted words need to be traversed, such as different name, before merging can be carried out

a1.forEach(item=>{
    if(!b.some(bitem=>{
        return bitem.name == item.name
    })){
        b.push(item)
    }
})
a2.forEach(item=>{
    if(!b.some(bitem=>{
        return bitem.name == item.name
    })){
        b.push(item)
    }
})

Object.assgin can solve the problem. The question is which value you want to keep


The syntax for

number combination and using ES6 is as follows:

var a = [1,2,3];
var b = [4,5,6];
var c = [...a,...b];
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3b5f4-2c26e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3b5f4-2c26e.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?