简体   繁体   English

嵌套的 Object.keys() 打印属性多次而不是一次

[英]Nested Object.keys() are printing properties multiple times instead of only once

I have two objects that I need to loop through so I can use their properties later on.我有两个需要循环的对象,以便以后可以使用它们的属性。 However if I print the variables each of them is printed twice.但是,如果我打印变量,每个变量都会打印两次。 I understand that because I have the Object.keys() inside other Object.keys() .我明白,因为我在其他 Object.keys( Object.keys()中有Object.keys() Is there any way to loop through these two objects and only get each variable one time?有没有办法循环遍历这两个对象并且只获取每个变量一次?

My code:我的代码:

Object.keys(newData.name).map(async key => {
    Object.keys(newData._temp.images).map(async keyImage => {
        console.log(newData.name[key].name,'printed 2x instead of once');
        console.log(newData._temp.images[keyImage].rawFile.preview, 'printed 2x instead of once');
    });
});

Thank you in advance.先感谢您。

your logic here of nesting the loops is wrong.您在这里嵌套循环的逻辑是错误的。

these 2 object does not seem to be connected to one another, meaning you do not need the data from the first loop in order to perform the other loops.这 2 个 object 似乎没有相互连接,这意味着您不需要第一个循环中的数据来执行其他循环。 just split it into 2 seperate loops, would save you both time and repititions:只需将其拆分为 2 个单独的循环,即可节省时间和重复次数:

let nameKeys = Object.keys(newData.name).map(key => newData.name[key].name);
let imagesKeys = Object.keys(newData._temp.images).map(keyImage => 
                 newData._temp.images[keyImage].rawFile.preview);

now you can access nameKeys and imageKeys whenever you want, and they will contain the values you previously logged.现在您可以随时访问nameKeysimageKeys ,它们将包含您之前记录的值。 My naming might be a bit off tho, feel free to change that:D我的命名可能有点偏离,请随意更改:D

Also, as others mentioned- no need for the async keyword... you do not perform any async operation inside (yet, at least. if thats what you're planning then go ahead and keep it).此外,正如其他人提到的那样 - 不需要async关键字......您不会在内部执行任何异步操作(但是,至少。如果这就是您的计划,那么 go 领先并保持它)。

These iterators do not need to be nested.这些迭代器不需要嵌套。 The second iterator is not looping through an item of the first iterator.第二个迭代器没有循环通过第一个迭代器的项目。

Object.keys(newData.name).forEach(key => {
    console.log(newData.name[key].name);
});
Object.keys(newData._temp.images).forEach(keyImage => {
    console.log(keyImage[keyImage].rawFile.preview);
});

If you are only iterested in outputting data, then .map() is not the right function to use because this is used when you care about the return value.如果您只对输出数据感兴趣,那么.map()不是正确的 function 使用,因为当您关心返回值时使用它。 Use .forEach() if you just want to loop through things.如果您只想循环遍历事物,请使用.forEach()

Also, the async keyword is not needed here.. unless you plan to do some async/await stuff in the loops later!此外,这里不需要async关键字.. 除非您打算稍后在循环中执行一些 async/await 的东西!

You could iterate over the indices once and then access the values in both arrays:您可以遍历索引一次,然后访问 arrays 中的值:

 const names = Object.keys(newData.name);
 const images = Object.keys(newData._temp.images);

for(let i = 0; i < Math.min(names.length, images.length); i++) {
  const name = names[i];
  const image = images[i];
  //...
}

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

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