如何遍历对象数组以查找ID然后返回兄弟键

时间:2015-07-10 19:21:25

标签: javascript arrays object for-loop

下面有一个复杂的对象数组,我有一个term_id来搜索。我试图找到匹配的term_id,然后从我找到ticker: name的同一个对象中返回关联的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"
        }
    }
]

你将如何做到这一点?有_lodash的简单方法吗?

4 个答案:

答案 0 :(得分: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));

答案 1 :(得分:2)

您可以使用Array.prototype.some。例如:

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;
}

这是JSFiddle

答案 2 :(得分:1)

希望这会对你有所帮助。这是一个函数,您可以将对象传递给您搜索的 term_id ,并返回找到的自动收录器名称:

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"]

答案 3 :(得分:1)

forEach()循环有效,但是一旦id匹配,就无法阻止它循环遍历整个对象。假设id是唯一的,那么性能更好的选项就是while循环:

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";
}

如果您的容器很大,您可能需要考虑这种优化。如果您更喜欢功能性方法,则可以使用some()every()完成类似的操作,两者都会在给定指定条件的情况下退出。