4

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?

4 Answers 4

2

// 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));

Sign up to request clarification or add additional context in comments.

3 Comments

Shouldn't your search function do something with the result? Like return it?
Thanks yeah the forEach works :) I need to Gist this, 4 mins
I had the return in the forEach which wasn't working. Fixed that and made it run as a code snippet. Check your console and hit run.
2

You can use Array.prototype.some for this. For example:

function find(arr, t) {
    var ticker = null;

    arr.some(function (doc) {
        var tagMatch = doc.tags.some(function (tag) {
            return tag.term_id === t;
        });

        if (tagMatch) {
            ticker = doc.ticker.name;
        }

        return tagMatch;
    });

    return ticker;
}

Here's a JSFiddle.

1 Comment

Very nice and clean :)
1

Hope this helps you. It's a function that you can pass your objects into and a term_id you search for and it returns found ticker names:

var objs = [
    {
        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 getTickerNamesById(objs,id){
  var foundNames = [];
  objs.forEach(function(obj){
    obj.tags.forEach(function(term){
      if(term.term_id===id)foundNames.push(obj.ticker.name);
    });
  });
  return foundNames;
}

getTickerNamesById(objs,3333); // ["ticker2"]

Comments

1

A forEach() loop works, though there is no way to prevent it from cycling through the entire object once the id is matched. Assuming the id's are unique, a option with better performance would be the while loop:

function findId(id,container) {
  var i = 0,
      j;

  while (i < container.length) {
    j = 0;
    while (j < container[i].tags.length) {
      if (container[i].tags[j].term_id === id) {
       return container[i].ticker.name;
      }
      j += 1;
    }
    i += 1;
  }
  throw "item not found";
}

If your containers will be large you may want to consider this optimization. If you preferred a functional approach, you could accomplish a similar thing with some() or every(), both of which exit out given a specified condition.

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.