【问题标题】:Adding an element to objects in array of objects Javascript向对象数组中的对象添加元素Javascript
【发布时间】:2020-06-28 00:48:33
【问题描述】:

问题很简单。我尝试了很多次,因此我在stackoverflow中寻求帮助。 我有一个名为 data 的对象数组,它是从 mongodb 数据库返回的数据。 格式类似于:

 let data = [
{ booking: [ ref], max_booking: 0 },
{ booking: [ref], max_booking: 3 },
{ booking: [ref], max_booking: 5 },
{booking: [ref], max_booking: 4}
];

根据请求,我添加了来自我的数据库的真实数据:

[ { booking: [],
_id: null,
__v: 0,
comment: 'this is a comment',
date: 2020-07-26T00:00:00.000Z,
event_detail: 'this is event detail',
location: 'Kthm',
max_booking: 5,
meal_option: [ [Object] ],
sponsor_address: 'xxxx',
sponsor_name: 'yyyy',
id: null },
{ booking: [],
_id: 5ef60fab6c164281aee9ae2e,
date: 2020-07-30T00:00:00.000Z,
location: 'Kthm',
comment: 'this is another',
sponsor_name: 'Micorosft',
sponsor_address: 'USA',
event_detail: 'this is event detail',
meal_option: [ [Object] ],
max_booking: 5,
__v: 0,
id: '5ef60fab6c164281aee9ae2e' },
{ booking: [],
_id: 5ef6131f6c164281aee9ae30,
date: 2020-07-30T00:00:00.000Z,
location: 'TAN TOCK SENG HOSPITAL',
comment: 'this is dsadasdas',
sponsor_name: 'Asus',
sponsor_address: 'Los Angeles, USAssadsad',
event_detail: 'this is event detail',
meal_option: [ [Object] ],
max_booking: 5,
__v: 0,
id: '5ef6131f6c164281aee9ae30' },

]

我想执行如下操作: 当 max_booking 与任何对象上的预订数组长度匹配时,应向该特定对象添加一个新元素。

{booking:[lets assume 5], max_booking: 5, result:1}

如果预订仍然少于,则还应添加一个新元素,但结果为 0,如

{booking:[lets asssume 3], max_booking:5, result:0}

我做的这个操作是:

    data.map((d, i) => {
     if (d.booking.length === d.max_booking) {
      console.log("Fully Booked in index", i);
       d.result = 1;
     } else {
       console.log("Not booked");
       d.result = 0;
      }
      console.log(d.result);
    });
  console.log(data[3]);

让我们暂时忽略 else 应该小于符号。 foreach 循环中 d.result 的值打印在控制台上,但是当我记录数据时,根本没有任何改变。 任何人都可以帮助我。提前致谢。

代码截图:

输出截图:

【问题讨论】:

  • 您是否在地图后记录data?还是您正在记录data.map(...) 的结果?
  • 如果您对.map() 的返回值不感兴趣,那么您不应该使用它。你想要.forEach()
  • @richytong 我记录了 data[4] 的值以测试结果。我已经编辑了问题
  • 您的数组只有四个元素,因此最后一个索引是3 而不是4。也就是说...您的脚本有效:jsfiddle.net/3x9cadr1(但请将.map() 替换为.forEach()
  • @Andreas 我有大量数据,但我的格式与我写的问题相似。我也试过 forEach() 。结果看起来一样

标签: javascript node.js arrays


【解决方案1】:

为什么人们要放弃一个简单的 for 循环?

for(var i=0;i<data.length;i++) {
     if (data[i].booking.length === data[i].max_booking) {
       console.log("Fully Booked in index", i);
       data[i].result = 1;
     } else {
       console.log("Not booked");
       data[i].result = 0;
     }
}
console.log(data.result);

【讨论】:

  • for(var i=0;i
  • 虽然没有解释 OP 的结果。即使使用(或滥用)map(),它也应该和你做同样的事情。
【解决方案2】:

您似乎忘记从地图中返回d。您必须从 map 回调中返回才能获取新数组。

const data = [
  { booking: [], max_booking: 0 },
  { booking: [{}, {}, {}], max_booking: 3 },
  { booking: [], max_booking: 5 },
  { booking: [{}, {}, {}], max_booking: 4 },
]

const mapped = data.map((d, i) => {
  if (d.booking.length === d.max_booking) {
    console.log("Fully Booked in index", i);
    d.result = 1;
  } else {
    console.log("Not booked");
    d.result = 0;
  }
  console.log(d.result);
  return d
})

console.log(mapped)

【讨论】:

  • OP 根本不使用返回值,因此 OP 没有从回调中返回任何内容是无关紧要的。对象被“就地”修改,并且 OPs 脚本有效。
【解决方案3】:

因为map()return a new array,如果你改变map()里面的数据,你应该像这样取回你的新数组:

var data = [{a:1},{a:2}]
data = data.map((d,i)=>{d.a = d.a+1; return d})

// Array [2]
// 0: Object { a: 2 }
​// 1: Object { a: 3 }
​

【讨论】:

  • 无论如何,当你改变对象时应该没关系。
猜你喜欢
  • 2011-08-11
  • 1970-01-01
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多