为了账号安全,请及时绑定邮箱和手机立即绑定

我怎样才能在javascript中返回两个对象

我怎样才能在javascript中返回两个对象

MYYA 2024-01-18 16:02:55
举个例子我有两个数组const tempData = [          { day: "Mon", temp: 33.6 },          { day: "Tue", temp: 34.6 },          { day: "Wed", temp: 33.1 },          { day: "Fri", temp: 35.6 }        ];const coughData = [          { day: "Mon", count: 2 },          { day: "Wed", count: 1 },          { day: "Thur", count: 1 },          { day: "Fri", count: 3 },          { day: "Sat", count: 1 }        ]; 我需要将这些数组合并为一个,以便如果日期匹配,则计数值添加到该对象,如果不匹配,则将两个对象添加到数组中。不知道解释是否不太清楚预期结果应该是这样的:const data = [          { day: "Mon", temp: 33.6, count: 2 },          { day: "Tue", temp: 34.6 },          { day: "Wed", temp: 33.1, count: 1 },          { day: "Thur", count: 1 },          { day: "Fri", temp: 35.6, count: 3 },          { day: "Sat", count: 1 }        ];我正在尝试像这样使用映射函数,但无法理解如果两个对象不匹配,如何返回它们:const data = tempData.map(temp => {          coughData.map(cough => {            if (temp.day === cough.day) {              return (temp.count = cough.count);            } else {              return cough;            }          });          return temp;        });
查看完整描述

5 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

day您可以收集对象中按分组的所有数据,并获取值作为结果集。


const

    addToCollection = (collection, key) => o => Object.assign(collection[o[key]] ??= {}, o),

    tempData = [{ day: "Mon", temp: 33.6 }, { day: "Tue", temp: 34.6 }, { day: "Wed", temp: 33.1 }, { day: "Fri", temp: 35.6 }],

    coughData = [{ day: "Mon", count: 2 }, { day: "Wed", count: 1 }, { day: "Thur", count: 1 }, { day: "Fri", count: 3 }, { day: "Sat", count: 1 }],

    collection = {};

    

tempData.forEach(addToCollection(collection, 'day'));

coughData.forEach(addToCollection(collection, 'day'));


console.log(Object.values(collection));

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2024-01-18
?
冉冉说

TA贡献1877条经验 获得超1个赞

您可以先合并数组的对象,然后.reduce()与Map一起使用来累积值。地图可以通过day属性作为键,这将允许您将相关的对象属性分组在一起。然后,您可以将Array.from()Map 转换回对象数组,如下所示:


const tempData = [{ day: "Mon", temp: 33.6 }, { day: "Tue", temp: 34.6 }, { day: "Wed", temp: 33.1 }, { day: "Fri", temp: 35.6 }];

const coughData = [{ day: "Mon", count: 2 }, { day: "Wed", count: 1 }, { day: "Thur", count: 1 }, { day: "Fri", count: 3 }, { day: "Sat", count: 1 }];


const arr = [...tempData, ...coughData];


const result = Array.from(arr.reduce((map, {day, ...rest}) => {

  const seen = map.get(day) || {day};

  return map.set(day, {...seen, ...rest});

}, new Map).values());


console.log(result);


查看完整回答
反对 回复 2024-01-18
?
慕神8447489

TA贡献1780条经验 获得超1个赞

const tempData = [

    { day: "Mon", temp: 33.6 },

    { day: "Tue", temp: 34.6 },

    { day: "Wed", temp: 33.1 },

    { day: "Fri", temp: 35.6 }

];

const coughData = [

    { day: "Mon", count: 2 },

    { day: "Wed", count: 1 },

    { day: "Thur", count: 1 },

    { day: "Fri", count: 3 },

    { day: "Sat", count: 1 }

];


const tempRes = [...tempData, ...coughData];

const result = tempRes.reduce((acc, curr) => {

    const { day, ...rest } = curr;

    acc[day] = acc[day] ? Object.assign({}, acc[day], rest) : curr

    return acc;

}, {})

console.log(Object.values(result))

.as-console-wrapper { max-height: 100% !important; top: 0; }


查看完整回答
反对 回复 2024-01-18
?
慕姐4208626

TA贡献1852条经验 获得超7个赞

let newArray = Array();

let longer = (tempData.length <= coughData.length) ? coughData.length : 

tempData.length;

for(let i = 0, j = 0; i < longer; ++i, ++j) {

    newArray.push(Object.assign(coughData[i], tempData[j]));

}

打印到控制台:


[ { day: 'Mon', count: 2, temp: 33.6 },

{ day: 'Tue', count: 1, temp: 34.6 },

{ day: 'Wed', count: 1, temp: 33.1 },

{ day: 'Fri', count: 3, temp: 35.6 },

{ day: 'Sat', count: 1 } ]


查看完整回答
反对 回复 2024-01-18
?
慕妹3146593

TA贡献1820条经验 获得超9个赞

您可以使用 Object.assign() javascript 函数。Object.assign()方法用于将一个或多个源对象的所有可枚举属性的值复制到一个目标对象。该方法将返回目标对象


就像这样:


const target = { a: 1, b: 2 };

const source = { b: 4, c: 5 };


const returnedTarget = Object.assign(target, source);


console.log(target);

// expected output: Object { a: 1, b: 4, c: 5 }


console.log(returnedTarget);

// expected output: Object { a: 1, b: 4, c: 5 }

https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/assign


查看完整回答
反对 回复 2024-01-18
  • 5 回答
  • 0 关注
  • 162 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信