简体   繁体   English

如何在 javascript 中返回两个对象

[英]How can i return two objects in javascript

As an example举个例子

I have two arrays我有两个 arrays

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

I need to merge these arrays into one so that if the day matches the count value adds to that object if it doesn't match it adds both objects to the array.我需要将这些 arrays 合并为一个,以便如果日期匹配,则计数值添加到 object 如果不匹配,则将两个对象添加到数组中。

Don't know if the explanation isn't so clear but不知道解释是不是很清楚,但是

The expected result should be like this:预期的结果应该是这样的:

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

I am trying to use map function like so but can't understand how do I return both the objects if they don't match:我正在尝试像这样使用 map function 但不明白如果它们不匹配我该如何返回这两个对象:

const data = tempData.map(temp => {
          coughData.map(cough => {
            if (temp.day === cough.day) {
              return (temp.count = cough.count);
            } else {
              return cough;
            }
          });
          return temp;
        });

You could collect all data grouped by day in an object and get the values as result set.您可以在 object 中收集按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; }

You can first merge the objects of the arrays, and then use .reduce() along with a Map to accumulate the values.您可以先合并 arrays 的对象,然后使用 .reduce .reduce()Map来累积值。 The Map can be keyed by the day property, which will allow you to group related object properties together. Map 可以由day属性键入,这将允许您将相关的 object 属性组合在一起。 You can then use Array.from() to transform your Map back into an array of objects like so:然后,您可以使用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);

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

Print to console:打印到控制台:

[ { 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 } ]

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

You can use Object.assign() javascript function.您可以使用 Object.assign() javascript function。 The Object.assign () method is used to copy the values of all the enumerable properties of one or more source objects to a target object. Object.assign() 方法用于将一个或多个源对象的所有可枚举属性的值复制到目标 object。 This method will return the target object此方法将返回目标 object

Like so:像这样:

 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 https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Object/assign

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM