Search code examples
javascriptobjectnested-loops

How do i iterate through a specific object in an array


i have an array with objects and i need to get just the values(not the keys) of the object "tipo".

const productsArray = [
    {
        id: 1,
        title: "Tacos",
        price: 30,
        score: 40,
        description: "Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
        tipo: {
             campechano: 25,
             costilla:25,
             cspaldilla:25,
             lomo:25,
             maciza:25,
             panza:25,
             pescuezo:25,
             surtida:25
        }
    },
    {
        id: 2,
        title: "Kilogramos",
        price: 500,
        score: 89,
        description: "Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
        tipo: {
            campechano: 500,
            costilla:50,
            cspaldilla:50,
            lomo:50,
            maciza:50,
            panza:50,
            pescuezo:50,
            surtida:50
       }
    },
    {
        id: 3,
        title: "Consome",
        price: 50,
        score: 25,
        description: "Delicioso Consome de Borrego con arroz y garbanzo",
        tipo: {
            chicho: 30,
            grande: 50
        }
    }
]

i tried these:

for (let key in productsArray){
    console.log(`${key}${productsArray[key]}`);
}

but i get

0[object 0bject]
1[object 0bject]
2[object 0bject]

instead of: 25 25 25 25 25 25 25 25 500 500 500 500 500 500 500 500 30 50 i can't figure out how to get these outputs, some tips?


Solution

  • You wanna use flatMap() and Object.values().

    const productsArray = [
      {
        id: 1,
        title: "Tacos",
        price: 30,
        score: 40,
        description:
          "Delicioso taco con barbacoa recien salida del horno sobre dos tortilla de maiz",
        tipo: {
          campechano: 25,
          costilla: 25,
          cspaldilla: 25,
          lomo: 25,
          maciza: 25,
          panza: 25,
          pescuezo: 25,
          surtida: 25,
        },
      },
      {
        id: 2,
        title: "Kilogramos",
        price: 500,
        score: 89,
        description:
          "Delicioso kilo de barbacoa hecha al horno con leña y sin hueso",
        tipo: {
          campechano: 500,
          costilla: 50,
          cspaldilla: 50,
          lomo: 50,
          maciza: 50,
          panza: 50,
          pescuezo: 50,
          surtida: 50,
        },
      },
      {
        id: 3,
        title: "Consome",
        price: 50,
        score: 25,
        description: "Delicioso Consome de Borrego con arroz y garbanzo",
        tipo: {
          chicho: 30,
          grande: 50,
        },
      },
    ];
    
    const result = productsArray.flatMap((x) => Object.values(x.tipo));
    console.log(result);
    /* Stackoverflow: show only console */
    .as-console-wrapper {
        max-height: 100% !important;
        top: 0;
    }

    Explanation

    For each item in your productsArray you want to have one item which is just the values of the tipo objects, which you get by using Object.values(). So to do that you would use map(), but as you also want to have all the values in a single list you need to flatten that, that's why you wanna use flatMap().