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

举个例子


我有两个数组


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;

        });


MYYA
浏览 163回答 5
5回答

拉丁的传说

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; }

冉冉说

您可以先合并数组的对象,然后.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);

慕神8447489

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; }

慕姐4208626

let newArray = Array();let longer = (tempData.length <= coughData.length) ? coughData.length :&nbsp;tempData.length;for(let i = 0, j = 0; i < longer; ++i, ++j) {&nbsp; &nbsp; 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 } ]

慕妹3146593

您可以使用 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript