简体   繁体   English

如何循环遍历同一个数组 object n 次

[英]How to loop through the same array object n number of times

I need to go through the same object and print it, according to the n number of meals, that the user has.我需要通过相同的 object go 并根据用户的 n 餐次数打印它。 I have the following arrays我有以下 arrays

[id, name, food1, food2, food3],
[id, name, food1, food2, ''],
[id, name, food1, '', '']

in the database it should be something like this:在数据库中它应该是这样的:

1 | Ronald| chicken rice     | NoFood1
2 | Ronald| Tortillas azadas | NoFood2
3 | Ronald| stewed chicken   | NoFood3
4 | Paul  | Arepas           | NoFood1
5 | Paul  | Torillas         | NoFood2
6 | Raul  | noodles          | NoFood1

Because the records are based on the amount of food per user, but with the code I have I bring me the first food of each object, I was seeing that there are examples with linkedlist and.push, but good if you have to use another for I do, but if you could show me more or less how it would be please with the code I have or if there is another implementation and I am very grateful for your comments.因为记录是基于每个用户的食物量,但是使用我的代码我给我带来了每个 object 的第一个食物,我看到有 linkedlist 和 .push 的例子,但是如果你必须使用另一个很好因为我知道,但是如果你能或多或少地告诉我我拥有的代码会如何,或者是否有其他实现,我非常感谢你的评论。

My function

ConditionDataToSend(lines, values: Array<any>){
while (lines !== null) {
const info: FoodDto = {food: '', name: '', id:0, noFood:''}

if (values.length >= 4 && values[4] !== '') {

    info.name = values[1]
    info.food = values[2]
    info.noFood = 'food1'
    info.id = id

    return info
}

if (values.length >= 5 && values[5] !== '') {
   
    info.name = values[1]
    info.food = values[2]
    info.nofood = 'food2'
    info.id = id

    return info
}

....

I'm slightly confused by the question (particularly the noFood property), but does this do what you want?我对这个问题(特别是noFood属性)有点困惑,但这是否符合您的要求?

const data = [
  [1, 'Jim', 'raspberry', 'blackberry', 'lingonberry'],
  [2, 'Tina', 'boysenberry', 'strawberry', ''],
  [3, 'Sarah', 'blueberry', '', '']
];

function infoTransform(values) {
  return values.map(value => {
    [id, name, ...foods] = value;

    return {
      id,
      name,
      foods: foods.filter(food => food !== '')
    };
  });
}

console.log(infoTransform(data));

/* Log:
[
  {
    id: 1,
    name: 'Jim',
    foods: [ 'raspberry', 'blackberry', 'lingonberry' ]
  },
  { id: 2, name: 'Tina', foods: [ 'boysenberry', 'strawberry' ] },
  { id: 3, name: 'Sarah', foods: [ 'blueberry' ] }
]
*/

EDIT: Updated code with noFood :编辑:使用noFood更新代码:

const data = [
  [1, 'Jim', 'raspberry', 'blackberry', 'lingonberry'],
  [2, 'Tina', 'boysenberry', 'strawberry', ''],
  [3, 'Sarah', 'blueberry', '', '']
];

function infoTransform(values) {
  return values.map(value => {
    // Destructure
    let [id, name, ...foods] = value;

    // Filter out empty strings
    foods = foods.filter(food => food !== '');

    // Set food and noFood properties
    let noFood = null;
    let food = null;

    if (foods.length === 1) {
      noFood = foods[0];
      food = foods[0];
    } else if (foods.length > 1) {
      noFood = foods[foods.length - 1];
      food = foods[0];
    }

    return {
      id,
      name,
      food,
      noFood
    };
  });
}

console.log(infoTransform(data));

/* Log:
[
  { id: 1, name: 'Jim', food: 'raspberry', noFood: 'lingonberry' },
  { id: 2, name: 'Tina', food: 'boysenberry', noFood: 'strawberry' },
  { id: 3, name: 'Sarah', food: 'blueberry', noFood: 'blueberry' }
]
*/

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

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