How to loop through Array of Objects to find ID then return a sibling key

Leon Gaban

I have a complex Array of Objects below, and I have a term_id to search on. I'm trying to find the matching term_id, then return the associated ticker: name from the same Object from which I found the term_id.

container = [Object, Object];

// container:
[
    0: Object {
        tags: [
            0: {
                term: "tag_name_1",
                term_id: 1111
            },
            0: {
                term: "tag_name_2",
                term_id: 2222
            }
        ],
        ticker: {
            name: "ticker1"
        }
    },
    1: Object {
        tags: [
            0: {
                term: "tag_name_3",
                term_id: 3333
            }
        ],
        ticker: {
            name: "ticker2"
        }
    }
]

How would you accomplish this? Is there an easy way with _lodash?

ChadF

// You can do this with native JS:

var container = [{tags: [{term: "tag_name_1",term_id: 1111},{term: "tag_name_2",term_id: 2222}],ticker: {name: "ticker1"}},{tags: [{term: "tag_name_3",term_id: 3333}],ticker: {name: "ticker2"}}];

function search (container, id) {
  var contains = false;
  var result;

  container.forEach(function(obj){
    obj.tags.forEach(function(innerData){
      if (innerData.term_id === id) {
        contains = true;
      }
    })
    if (contains) {
      result = obj.ticker.name;
      contains = false;
    }
  });

  return result;
}

console.log(search(container, 1111));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to loop through array and find if key

From Dev

Javascript: loop through array of objects to find key value pair and render them in the table td

From Dev

Loop through array of objects and return sum of each total values by object element id

From Dev

How to loop through an array of objects in swift

From Dev

How to loop through array of objects in angularjs

From Dev

Loop through arrays of objects and return sum by ID reference

From Dev

Loop through an array of objects?

From Dev

how to Loop through an array with a custom key

From Dev

Javascript loop through an array of objects and return the first value

From Dev

How to loop through an array to find the closest distance?

From Dev

Loop through array of objects to find object with matching property

From Dev

Loop through array of objects to find if one value differs

From Dev

How to find if a specific key is true in Array of objects

From Dev

Ruby use find to loop through an array and return the matching value

From Dev

loop through array to find matches and return every possible solution

From Dev

How to find object by id in array of objects?

From Dev

How to find object by id in array of objects?

From Dev

Loop through array to find all objects with matching ids, and merge those objects into one element in the array.

From Dev

How to loop through an array and find matches with the values in the array

From Dev

How can I loop through an array of JSON objects?

From Dev

PHP - How to loop through array of objects and sum values

From Dev

How to loop through an array of objects and nested object in vue 2

From Dev

Loop through Array of Objects within Objects in JavaScript

From Dev

Javascript loop through several objects and return true if

From Dev

Loop through array of objects and add to Array of arrays

From Dev

How to loop through array to find 4 identical values that are consecutive?

From Dev

Could you help me find a way how to loop through an array?

From Dev

Could you help me find a way how to loop through an array?

From Dev

How to loop through an array of objects, that's within another array of objects - javascript

Related Related

  1. 1

    How to loop through array and find if key

  2. 2

    Javascript: loop through array of objects to find key value pair and render them in the table td

  3. 3

    Loop through array of objects and return sum of each total values by object element id

  4. 4

    How to loop through an array of objects in swift

  5. 5

    How to loop through array of objects in angularjs

  6. 6

    Loop through arrays of objects and return sum by ID reference

  7. 7

    Loop through an array of objects?

  8. 8

    how to Loop through an array with a custom key

  9. 9

    Javascript loop through an array of objects and return the first value

  10. 10

    How to loop through an array to find the closest distance?

  11. 11

    Loop through array of objects to find object with matching property

  12. 12

    Loop through array of objects to find if one value differs

  13. 13

    How to find if a specific key is true in Array of objects

  14. 14

    Ruby use find to loop through an array and return the matching value

  15. 15

    loop through array to find matches and return every possible solution

  16. 16

    How to find object by id in array of objects?

  17. 17

    How to find object by id in array of objects?

  18. 18

    Loop through array to find all objects with matching ids, and merge those objects into one element in the array.

  19. 19

    How to loop through an array and find matches with the values in the array

  20. 20

    How can I loop through an array of JSON objects?

  21. 21

    PHP - How to loop through array of objects and sum values

  22. 22

    How to loop through an array of objects and nested object in vue 2

  23. 23

    Loop through Array of Objects within Objects in JavaScript

  24. 24

    Javascript loop through several objects and return true if

  25. 25

    Loop through array of objects and add to Array of arrays

  26. 26

    How to loop through array to find 4 identical values that are consecutive?

  27. 27

    Could you help me find a way how to loop through an array?

  28. 28

    Could you help me find a way how to loop through an array?

  29. 29

    How to loop through an array of objects, that's within another array of objects - javascript

HotTag

Archive