Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript:比较2个对象数组并删除元素_Javascript_Arrays_Object - Fatal编程技术网

Javascript:比较2个对象数组并删除元素

Javascript:比较2个对象数组并删除元素,javascript,arrays,object,Javascript,Arrays,Object,我有2个具有用户购买计数的数组对象。我试图比较这两个阵列,看看哪个购买的产品有更多的购买数量。如果newUser的计数较大,则将删除oldUseroldUsers对象,反之亦然 例如,对于newUser而言,shoes count=2,对于oldUser而言,shoes count=4.5,因此它将从newUser数组中删除,如newUserOutput 我拥有的 我目前可以获得交叉点来比较每个阵列中的购买产品,但我不知道如何删除计数更大的交叉点或输出两个单独的阵列 我尝试过的: con

我有2个具有用户购买计数的数组对象。我试图比较这两个阵列,看看哪个购买的产品有更多的购买数量。如果
newUser
的计数较大,则将删除
oldUser
oldUsers对象,反之亦然

例如,对于
newUser
而言,shoes count=2,对于
oldUser
而言,shoes count=4.5,因此它将从newUser数组中删除,如
newUserOutput

我拥有的

我目前可以获得交叉点来比较每个阵列中的购买产品,但我不知道如何删除计数更大的交叉点或输出两个单独的阵列

我尝试过的:

    const newUser = [
    { count: 2, purchased: "shoes"}, // smaller then oldUser
    { count: 2, purchased: "iphone"},
    { count: 2, purchased: "cup"},// greater then oldUser
    { count: 1, purchased: "charger"},// smaller then oldUser
    { count: 1, purchased: "plant"}, 

]
const oldUser = [
    { count: 4.5, purchased: "shoes"},// greater then newUser
    { count: 3.5, purchased: "charger"},// greater then newUser
    { count: 3.5, purchased: "macbook"},
    { count: 1, purchased: "cup"}, // smaller then newUser
]

const handleIntersection = () =>{
    const results = newUser.filter(({ purchased: id1 }) => !oldUser.some(({ purchased: id2 }) => id2 === id1));
    console.log(results)
    // I don't know how to get the two separe array outputs. 
    //I have tried to find the intersection of each purchased item
}
预期产出

const newUserOutput = [
    { count: 2, purchased: "iphone"},
    { count: 2, purchased: "cup"},
    { count: 1, purchased: "plant"},
]

const oldUserOutput = [
    { count: 4.5, purchased: "shoes"},
    { count: 3.5, purchased: "charger"},
    { count: 3.5, purchased: "macbook"},
]

您可以收集更多的购买,并生成一个新的结果集,其中只包含最伟大的值

const
newUser=[{count:2,购买:“鞋子”},{count:2,购买:“iphone”},{count:2,购买:“杯子”},{count:1,购买:“充电器”},{count:1,购买:“植物”}],
oldUser=[{计数:4.5,购买的:“鞋子”},{计数:3.5,购买的:“充电器”},{计数:3.5,购买的:“macbook”},{计数:1,购买的:“杯子”},
结果={},
greaters=Object.entries({newUser,oldUser}).reduce((r[k,a])=>{
a、 forEach(o=>{
如果(!r[o.purchased]| r[o.purchased].o.count(result[k]??=[]).push(o));
控制台日志(结果)

.as console wrapper{max height:100%!important;top:0;}
您可以创建
newUserOutput
并从oldUser筛选
newUserOutput
元素

const newUser=[
{count:2,购买:“shoes”},//比oldUser小
{count:2,购买:“iphone”},
{count:2,购买:“cup”},//大于oldUser
{count:1,购买:“charger”},//比oldUser小
{计数:1,购买:“植物”},
]
常量用户=[
{count:4.5,购买:“shoes”},//大于newUser
{计数:3.5,购买:“充电器”},//大于newUser
{计数:3.5,购买:“macbook”},
{count:1,购买:“cup”},//比newUser小
]
const newUserUpdated=newUser.filter((项)=>{
const found=oldUser.find((a)=>a.purchased==item.purchased);
返回find?find.count{
const found=newUserUpdated.find((a)=>a.purchased==item.purchased);
返回找到?假:真;
} )
console.log({newUserUpdated,oldUserUpdated})