简体   繁体   English

与另一个数组比较后如何添加缺失对象的值?

[英]How to add value of missing object after comparing to another array?

I have an array of months ( var months ), and I am trying to filter out and get the months that the array of object ( var array1 ) does not have and add empty value of property Avg我有一个月份数组( var months ),我试图过滤掉并获取对象数组( var array1 )没有的月份并添加属性Avg空值

var months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];

var array1 = [{Month: 'Jan', Avg: 10},{Month: 'Feb', Avg: 20},
{Month: 'May', Avg: 50},{Month: 'Jun', Avg: 60}];`

Eg.例如。 array1 has the property value of Month: Jan , Feb , May and Jun , but I want to get the months that are missing and add value of 0 to property Avg like {Month: 'Mar', Avg: 0}, {Month: 'Apr', Avg: 0}, ... for the missing months comparing it with months array array1的属性值为Month: Jan , Feb , MayJun ,但我想获取缺失的月份并将值 0 添加到属性Avg例如{Month: 'Mar', Avg: 0}, {Month: 'Apr', Avg: 0}, ...对于缺少的月份,将其与months数组进行比较

Expected result of array1: array1 的预期结果:

[{Month: 'Jan', Avg: 10}, {Month: 'Feb', Avg: 20}, 
{Month: 'Mar', Avg: 0}, {Month: 'Apr', Avg: 0}, 
{Month: 'May', Avg: 50}, {Month: 'Jun', Avg: 60}, 
{Month: 'Jul', Avg: 0}, {Month: 'Aug', Avg: 0},
{Month: 'Sep', Avg: 0}, {Month: 'Oct', Avg: 0}, 
{Month: 'Nov', Avg: 0}, {Month: 'Dec', Avg: 0}];

Use Array.map with Array.find使用Array.mapArray.find

 const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; const array1 = [{Month: 'Jan', Avg: 10},{Month: 'Feb', Avg: 20}, {Month: 'May', Avg: 50},{Month: 'Jun', Avg: 60}]; const result = months.map(m => array1.find(a => a.Month === m)??{Month: m, Avg: 0}); console.log(result);

Reference: Array , Nullish参考: ArrayNullish

I think this should work pretty much!我认为这应该很有效!

 var months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ]; var array1 = [{ Month: "Jan", Avg: 10 }, { Month: "Feb", Avg: 20 }, { Month: "May", Avg: 50 }, { Month: "Jun", Avg: 60 }, ]; let finalArr = []; months.filter((e) => { let index = array1.findIndex((ele) => ele.Month == e); if (index >= 0) { finalArr.push(array1[index]); } else { finalArr.push({ Month: e, Avg: 0 }); } }); console.log(finalArr);

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

相关问题 如何将值从数组中的对象添加到另一个数组中的另一个对象? - How to add value from object in an array into another object in another array? 通过比较 object 的两个数组添加丢失的对象 - Add the missing Object's by comparing Two array of object 将键/值数组中的对象与另一个对象(键/值)进行比较 - Comparing objects from key/value array with another object (Key/value) 比较数组值和对象 - Comparing array value with object 检查另一个数组后如何为数组添加值 - How to add value to array after checking another array 如何通过与模型比较将特定缺少的字段添加到json对象 - How to add specific missing fields to a json object by comparing with a model 如何在比较数组和 object 时检查缺失的字段? - How do I check the missing fields while comparing an array and an object? 如何检测正确的 object 比较另一个阵列中的相同 object? - How to detect correct object comparing same object in another array? 如何通过将数组值与 Vuejs 中的另一个数组进行比较来迭代数组值? - How to iterate array value by comparing it with another array in Vuejs? 我如何通过比较 ReactJs 中的另一个数组来呈现数组值 - How do i render the Array value by comparing another array in ReactJs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM