简体   繁体   English

缺少要添加到 json object 密钥的日期

[英]Missing dates to add in a json object of keys

I have two arrays one with constant dates and other with multiple JSON objects of dates, but the JSON objects have some missing dates how to add the missing dates from constant dates array to the second array of JSON objects I have two arrays one with constant dates and other with multiple JSON objects of dates, but the JSON objects have some missing dates how to add the missing dates from constant dates array to the second array of JSON objects

constantdates =  [
    'Jul 7', 
    'Jul 8',
    'Jul 9', 
    'Jul 10',
    'Jul 27', 
    'Jul 16',
    'Jul 30', 
    'Jul 26',
    'Jul 12', 
    'Jul 22',
    'Jul 17',
    'Jul 24',
    'Jul 19', 
    'Jul 14',
    'Jul 25', 
    'Jul 13',
    'Jul 23', 
    'Jul 15',
    'Jul 29', 
    'Jul 11',
    'Jul 28', 
    'Jul 21',
    'Jul 20', 
    'Jul 18'
  ]

myarray = [{
    name: 'Arun',
    address: 'hydrabad',
    worker: 'test',
    'Jul 11': 1425,
    'Jul 15': 1425,
    'Jul 16': 1475,
    'Jul 17': 1425,
    'Jul 21': 1475,
    'Jul 22': 1425,
    'Jul 23': 1475,
    'Jul 24': 1225,
    'Jul 25': 1425,
    'Jul 26': 1075,
    'Jul 27': 1475,
    'Jul 29': 1425,
    'Jul 30': 1325,
  },
  {
    name: 'Abi',
    address: 'delhi',
    worker: 'test',
    'Jul 11': 1425,
    'Jul 12': 1275,
    'Jul 13': 1475,
    'Jul 14': 1275,
    'Jul 15': 1425,
    'Jul 16': 1475,
    'Jul 17': 1425,
    'Jul 18': 1425,
    'Jul 19': 1475,
    'Jul 21': 1475,
    'Jul 22': 1425,
    'Jul 23': 1475,
    'Jul 24': 1225,
    'Jul 25': 1425,
    'Jul 26': 1075,
    'Jul 27': 1475,
    'Jul 29': 1425,
    'Jul 30': 1325
  }]

Expected Output预计 Output

 [{
    name: 'Arun',
    address: 'hydrabad',
    worker: 'test',
    'Jul 7': null, 
    'Jul 8': null,
    'Jul 9': null, 
    'Jul 10': null,
    'Jul 11': 1425,
    'Jul 12': null,
    'Jul 13': null,
    'Jul 14': null,
    'Jul 15': 1425,
    'Jul 16': 1475,
    'Jul 17': 1425,
    'Jul 18': null,
    'Jul 19': null,
    'Jul 20': null,
    'Jul 21': 1475,
    'Jul 22': 1425,
    'Jul 23': 1475,
    'Jul 24': 1225,
    'Jul 25': 1425,
    'Jul 26': 1075,
    'Jul 27': 1475,
    'Jul 28': null,
    'Jul 29': 1425,
    'Jul 30': 1325,
  },
  {
    name: 'Abi',
    address: 'delhi',
    worker: 'test',
    'Jul 7': null, 
    'Jul 8': null,
    'Jul 9': null, 
    'Jul 10': null,
    'Jul 11': 1425,
    'Jul 12': 1275,
    'Jul 13': 1475,
    'Jul 14': 1275,
    'Jul 15': 1425,
    'Jul 16': 1475,
    'Jul 17': 1425,
    'Jul 18': 1425,
    'Jul 19': 1475,
    'Jul 20': null,
    'Jul 21': 1475,
    'Jul 22': 1425,
    'Jul 23': 1475,
    'Jul 24': 1225,
    'Jul 25': 1425,
    'Jul 26': 1075,
    'Jul 27': 1475,
    'Jul 28': null,
    'Jul 29': 1425,
    'Jul 30': 1325
  }]

Use reduce .使用reduce Inside reduce callback use forEach on the dates array.在日期数组上减少回调使用forEach Inside forEach callback take each element from dates array and check if same element exist in the object of myArray.forEach回调中,从日期数组中获取每个元素,并检查 myArray 的 object 中是否存在相同的元素。 If it does not exist then add it as a new key and set value to null.如果它不存在,则将其添加为新键并将值设置为 null。

 const dates = [ 'Jul 7', 'Jul 8', 'Jul 9', 'Jul 10', 'Jul 27', 'Jul 16', 'Jul 30', 'Jul 26', 'Jul 12', 'Jul 22', 'Jul 17', 'Jul 24', 'Jul 19', 'Jul 14', 'Jul 25', 'Jul 13', 'Jul 23', 'Jul 15', 'Jul 29', 'Jul 11', 'Jul 28', 'Jul 21', 'Jul 20', 'Jul 18' ] const myarray = [{ name: 'Arun', address: 'hydrabad', worker: 'test', 'Jul 11': 1425, 'Jul 15': 1425, 'Jul 16': 1475, 'Jul 17': 1425, 'Jul 21': 1475, 'Jul 22': 1425, 'Jul 23': 1475, 'Jul 24': 1225, 'Jul 25': 1425, 'Jul 26': 1075, 'Jul 27': 1475, 'Jul 29': 1425, 'Jul 30': 1325, }, { name: 'Abi', address: 'delhi', worker: 'test', 'Jul 11': 1425, 'Jul 12': 1275, 'Jul 13': 1475, 'Jul 14': 1275, 'Jul 15': 1425, 'Jul 16': 1475, 'Jul 17': 1425, 'Jul 18': 1425, 'Jul 19': 1475, 'Jul 21': 1475, 'Jul 22': 1425, 'Jul 23': 1475, 'Jul 24': 1225, 'Jul 25': 1425, 'Jul 26': 1075, 'Jul 27': 1475, 'Jul 29': 1425, 'Jul 30': 1325 } ] const newData = myarray.reduce((acc, curr) => { let obj = {} obj['fullName'] = curr.name; obj['address'] = curr.address; obj['worker'] = curr.worker; dates.forEach((item) => { if (curr.hasOwnProperty(item)) { obj[item] = curr[item] } else { obj[item] = null } }) acc.push(obj) return acc; }, []); console.log(newData)

Using no dependencies, and one line:不使用依赖项,只有一行:

myarray = myarray.map(w => Object.assign(constantdates.reduce((a, b) => (a[b] = null, a), {}), w))

The reduction takes the array of dates and makes it like { 'Date': null }, and then we use Object.assign to copy the original value onto those keys.减少采用日期数组并使其类似于 { 'Date': null },然后我们使用Object.assign将原始值复制到这些键上。

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

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