0

I'm currently looping on an object in javascript and successfully got what I need from the _source level. My problem now is that there is another array under color inside _source.

Currently, console.log(searchResult); gives me this object:

1
  _source
    color
      1
        color_type
    type
      name
        id

I can access type and color because they're on the _source level, but I need to access color_type which is part of another array within color and it needs to obviously be the info contained in my originally indexed element.

Do I need to create a new loop here to access the color info?

    let searchResult = response.hits.hits;
    console.log(searchResult);
    for(let i = 0; i < searchResult.length; i++) {           

                //This line displays the name.id properly
                document.getElementById("name").value = searchResult[i]._source.type.name.id;   

                //this line gives undefined         
                document.getElementById("color").value = searchResult[i]._source.color[i].color_type;
        })  
    }
8
  • Could you please share your object with us? Commented Aug 29, 2018 at 16:45
  • 1
    Basically yes because _source.color is an array that can contain multiple elements, but ultimately it depends on what you are trying to do. Commented Aug 29, 2018 at 16:45
  • Are you only ever interested in the first colour, where present? Commented Aug 29, 2018 at 16:46
  • Yes, in this case only the first @Rup Commented Aug 29, 2018 at 16:50
  • Is 1 the only index inside color? Commented Aug 29, 2018 at 16:53

3 Answers 3

1

Since you are only interested in the second element (index 1), this should do the trick:

document.getElementById("color").value = searchResult[i]._source.color[1].color_type;
Sign up to request clarification or add additional context in comments.

3 Comments

So, this is weird but it does apply the proper value however it says 'cannot read property 1 of undefined?
Tom, first element in an array starts with index 0 and not 1. :)
Please look at my answer below
1

Since color is also an array and you are trying color[i], it is looking for multiple indexes in color array through the index of searchResult array.

If you are only interested in the first element of color array, you should do this:

document.getElementById("color").value = searchResult[i]._source.color[0].color_type;

Instead of assigning i, you assign 0 to get the first color only.

If you are looking for multiple colors, you need to create a separate loop that only loops from color[0] to color[color.length]

Comments

1

If you're sure the key in the color will always be 1, just use:

document.getElementById("color").value = searchResult[i]._source.color[1].color_type;

If you're not sure, use:

for (let i = 0; i < searchResult.length; i++) {           

    document.getElementById("name").value = searchResult[i]._source.type.name.id;   

    for (let j = 0; j < searchResult[i]._source.color.length; j++) { 

        // this set to the #color element the first not undefined color_type from color array
        if (searchResult[i]._source.color[j].color_type != undefined) {

            document.getElementById("color").value = searchResult[i]._source.color[j].color_type;
            break;

        }         

    } 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.