将属性添加到对象而不使用展开操作符。

huangapple go评论70阅读模式
英文:

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({
  &#39;0&#39;: { &#39;hangWithFriends&#39;: 100 },
  &#39;1&#39;: { &#39;Chill&#39;: 30, &#39;Book&#39;: 80 },
  &#39;2&#39;: { &#39;Yoga&#39;: 40 },
  &#39;3&#39;: { &#39;Birthday&#39;: 200 },
  &#39;4&#39;: { &#39;Gas&#39;: 70 },
  &#39;5&#39;: { &#39;Rent&#39;: 900, &#39;testingsub&#39;: 200 },
  &#39;6&#39;: { &#39;testSub&#39;: 500 }
}));

const dataArr = [...dataMap.entries()]
  .map(([key, value]) =&gt; ({ date: +key, ...value }))
  .sort((a, b) =&gt; 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({
  &#39;0&#39;: { &#39;hangWithFriends&#39;: 100 },
  &#39;1&#39;: { &#39;Chill&#39;: 30, &#39;Book&#39;: 80 },
  &#39;2&#39;: { &#39;Yoga&#39;: 40 },
  &#39;3&#39;: { &#39;Birthday&#39;: 200 },
  &#39;4&#39;: { &#39;Gas&#39;: 70 },
  &#39;5&#39;: { &#39;Rent&#39;: 900, &#39;testingsub&#39;: 200 },
  &#39;6&#39;: { &#39;testSub&#39;: 500 }
}));

const dataArr = [];

for (let [key, value] of dataMap) {
  dataArr.push({ date: +key, ...value });
}

dataArr.sort((a, b) =&gt; 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]) =&gt; {
  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 = {
  &#39;0&#39;: {
    &#39;hangWithFriends&#39;: 100
  },
  &#39;1&#39;: {
    &#39;Chill&#39;: 30,
    &#39;Book&#39;: 80
  },
  &#39;2&#39;: {
    &#39;Yoga&#39;: 40
  },
  &#39;3&#39;: {
    &#39;Birthday&#39;: 200
  },
  &#39;4&#39;: {
    &#39;Gas&#39;: 70
  },
  &#39;5&#39;: {
    &#39;Rent&#39;: 900,
    &#39;testingsub&#39;: 200
  },
  &#39;6&#39;: {
    &#39;testSub&#39;: 500
  }
};

let data = [];
for (let x in dataMap) {
  let obj = {
    date: Number(x),
    ...dataMap[x]
  };
  data.push(obj);
};

console.log(data);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月6日 21:35:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76629421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定