英文:
adding in props to obj without spreading
问题
将这个代码部分从英文翻译成中文:
      Map {
        0: { hangWithFriends: 100 },
        1: { Chill: 30, Book: 80 },
        2: { Yoga: 40 },
        3: { Birthday: 200 },
        4: { Gas: 70 },
        5: { Rent: 900, testingsub: 200 },
        6: { testSub: 500 }
      }
翻译为:
    [
         {date: 0, hangWithFriends: 100}, 
         {date:1, Chill: 30, Book:80}, 
         {date: 2, Yoga:40}, 
         {date: 3, Birthday:200}, 
         {date:4, Gas:70},  
         {date:5, Rent:900, testingsub:200}, 
         {date: 6, testSub: 500}
    ]
翻译完成,没有其他内容。
英文:
Does anyone know how to turn this:
  Map {
    0: { hangWithFriends: 100 },
    1: { Chill: 30, Book: 80 },
    2: { Yoga: 40 },
    3: { Birthday: 200 },
    4: { Gas: 70 },
    5: { Rent: 900, testingsub: 200 },
    6: { testSub: 500 }
  }
into this:
[
     {date: 0, hangWithFriends: 100}, 
     {date:1, Chill: 30, Book:80}, 
     {date: 2, Yoga:40}, 
     {date: 3, Birthday:200}, 
     {date:4, Gas:70},  
     {date:5, Rent:900, testingsub:200}, 
     {date: 6, testSub: 500}
]
I tried this:
  reducedMonths.forEach((date, obj) => {
    newData.push({ date, ...obj });
  });
But got this and I know it's because I'm spreading but I don't know how else to add a dynamic amount of props:
  Array(7) [
    { date: { hangWithFriends: 100 } }, 
    { date: { Chill: 30, Book: 80 } }, 
    { date: { Yoga: 40 } }, { date: { Birthday: 200 } },
    { date: { Gas: 70 } }, 
    { date: { Rent: 900, testingsub: 200 } }, 
    { date: { testSub: 500 } }
  ]
答案1
得分: 1
你可以将地图条目的迭代器扩展为一个数组,并将键映射为解析后的日期整数,然后扩展值。
确保最后对日期进行排序,因为排序顺序不被保证。
const dataMap = new Map(Object.entries({
  '0': { 'hangWithFriends': 100 },
  '1': { 'Chill': 30, 'Book': 80 },
  '2': { 'Yoga': 40 },
  '3': { 'Birthday': 200 },
  '4': { 'Gas': 70 },
  '5': { 'Rent': 900, 'testingsub': 200 },
  '6': { 'testSub': 500 }
}));
const dataArr = [...dataMap.entries()]
  .map(([key, value]) => ({ date: +key, ...value }))
  .sort((a, b) => a.date - b.date);
console.log(dataArr);
如果你想要优化这个代码,你可以使用一个迭代器来跳过将条目转换为数组,然后调用 Array.prototype.map。
const dataMap = new Map(Object.entries({
  '0': { 'hangWithFriends': 100 },
  '1': { 'Chill': 30, 'Book': 80 },
  '2': { 'Yoga': 40 },
  '3': { 'Birthday': 200 },
  '4': { 'Gas': 70 },
  '5': { 'Rent': 900, 'testingsub': 200 },
  '6': { 'testSub': 500 }
}));
const dataArr = [];
for (let [key, value] of dataMap) {
  dataArr.push({ date: +key, ...value });
}
dataArr.sort((a, b) => a.date - b.date); // 确保你排序
console.log(dataArr);
英文:
You could spread the map entries iterator into an array, and map the key to a parsed date integer and spread the values.
Make sure you sort the dates at the end, because sorting order is not guarenteed.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const dataMap = new Map(Object.entries({
  '0': { 'hangWithFriends': 100 },
  '1': { 'Chill': 30, 'Book': 80 },
  '2': { 'Yoga': 40 },
  '3': { 'Birthday': 200 },
  '4': { 'Gas': 70 },
  '5': { 'Rent': 900, 'testingsub': 200 },
  '6': { 'testSub': 500 }
}));
const dataArr = [...dataMap.entries()]
  .map(([key, value]) => ({ date: +key, ...value }))
  .sort((a, b) => a.date - b.date);
  
console.log(dataArr);
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
If you want to optimize this, you can use an iterator to skip converting the entries into an array, prior to calling Array.prototype.map.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const dataMap = new Map(Object.entries({
  '0': { 'hangWithFriends': 100 },
  '1': { 'Chill': 30, 'Book': 80 },
  '2': { 'Yoga': 40 },
  '3': { 'Birthday': 200 },
  '4': { 'Gas': 70 },
  '5': { 'Rent': 900, 'testingsub': 200 },
  '6': { 'testSub': 500 }
}));
const dataArr = [];
for (let [key, value] of dataMap) {
  dataArr.push({ date: +key, ...value });
}
dataArr.sort((a, b) => a.date - b.date); // Make sure you sort
console.log(dataArr);
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
答案2
得分: 0
I think this Snippet might help you
const yourMap = {
  0: { hangWithFriends: 100 },
  1: { Chill: 30, Book: 80 },
  2: { Yoga: 40 },
  3: { Birthday: 200 },
  4: { Gas: 70 },
  5: { Rent: 900, testingsub: 200 },
  6: { testSub: 500 },
};
const transformedArray = Object.entries(yourMap).reduce((acc, [date, values]) => {
  const result = { date: parseInt(date, 10), ...values };
  acc.push(result);
  return acc;
}, []);
console.log(transformedArray);
英文:
I think this Snippet might help you
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const yourMap = {
  0: { hangWithFriends: 100 },
  1: { Chill: 30, Book: 80 },
  2: { Yoga: 40 },
  3: { Birthday: 200 },
  4: { Gas: 70 },
  5: { Rent: 900, testingsub: 200 },
  6: { testSub: 500 },
};
const transformedArray = Object.entries(yourMap).reduce((acc, [date, values]) => {
  const result = { date: parseInt(date, 10), ...values };
  acc.push(result);
  return acc;
}, []);
console.log(transformedArray);
<!-- end snippet -->
答案3
得分: 0
const dataMap = {
  '0': {
    '与朋友聚会': 100
  },
  '1': {
    '休闲': 30,
    '阅读': 80
  },
  '2': {
    '瑜伽': 40
  },
  '3': {
    '生日': 200
  },
  '4': {
    '加油费': 70
  },
  '5': {
    '租金': 900,
    '测试子项': 200
  },
  '6': {
    '测试子项目': 500
  }
};
let data = [];
for (let x in dataMap) {
  let obj = {
    日期: Number(x),
    ...dataMap[x]
  };
  data.push(obj);
};
console.log(data);
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const dataMap = {
  '0': {
    'hangWithFriends': 100
  },
  '1': {
    'Chill': 30,
    'Book': 80
  },
  '2': {
    'Yoga': 40
  },
  '3': {
    'Birthday': 200
  },
  '4': {
    'Gas': 70
  },
  '5': {
    'Rent': 900,
    'testingsub': 200
  },
  '6': {
    'testSub': 500
  }
};
let data = [];
for (let x in dataMap) {
  let obj = {
    date: Number(x),
    ...dataMap[x]
  };
  data.push(obj);
};
console.log(data);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论