0

I'm parsong RSS news using javascript.. The object returned (from Yahoo YQL) has the news inside a news object.

Normally, it's inside this subobject:

news.results.item

So I would just iterate normally using:

news.results.item.forEach

Now it gets interesting when I'm loading multiple sources.. it can return stuff like

news.results.item (array[10])
news.results.entry (array[10])
news.results.banana (array[10])

My question is, how can I iterate inside these entries when I don't know the naming that will be returned.. is there a simple way to merge them all? (Using jQuery is fine)

sigmaxf
  • 7,998
  • 15
  • 65
  • 125

3 Answers3

2

You can loop through all of the array properties on news.results:

var news = {
    results: {
        item: ["a", "b", "c"],
        entry: ["d", "e", "f"],
        banana: ["g", "h", "i"]
    }
};
Object.keys(news.results).forEach(function(key) {
    var value = news.results[key];
    if (Array.isArray(value)) {
        value.forEach(function(entry) {
            console.log(entry);
        });
    }
});

And if you're looking for something specific and want to stop when you find it, you can use some instead of forEach or do a nested find (you'll need a find polyfill for some browsers still).

But if you want to combine them before searching, that's fairly easily done:

var news = {
    results: {
        item: ["a", "b", "c"],
      entry: ["d", "e", "f"],
        banana: ["g", "h", "i"]
    }
};
var all = [];
Object.keys(news.results).forEach(function(key) {
    var value = news.results[key];
    if (Array.isArray(value)) {
        all.push.apply(all, value);
    }
});
all.forEach(function(entry) {
  console.log(entry);
});

(Some people would use reduce for that. I think it obscures rather than aiding reading in cases like this where the accumulation value never actually changes.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

You can use Object.keys(), for..of loop

let news = {
  result: {
    item: [1,2,3],
    entry: [4,5,6],
    banana: [7,8,9]
  }
}

for (let key of Object.keys(news.result)) {
  console.log(`${key}:`);
  for (let entries of news.result[key]) {
    console.log(entries)
  }
}
guest271314
  • 1
  • 15
  • 104
  • 177
  • @T.J.Crowder Not certain what you mean? OP is expecting possibly more than single property at `result` object? E.g., `result` could contain `item`, `entry` and `banana`? – guest271314 Nov 10 '16 at 17:40
  • @T.J.Crowder See updated post. – guest271314 Nov 10 '16 at 17:44
  • Yes, that fixes it. Beware of browser support for `Object.entries`, it's an ES2017 thing. – T.J. Crowder Nov 10 '16 at 17:45
  • @T.J.Crowder See updated post. `Object.keys()` returns expected result by adjusting pattern. Initially interpreted Question as only one property would be set at object. – guest271314 Nov 10 '16 at 17:51
0

Updated answer as pointed out by T.J. Crowder

var news = {
  results: {
    item: ["a", "b", "c"],
    entry: ["d", "e", "f"],
    banana: ["g", "h", "i"]
  }
};

for (var key in news.results) {
  console.log(key + " " + news.results[key])

}
Sagar
  • 538
  • 3
  • 9