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)

0

3 Answers 3

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.)

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

Comments

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

4 Comments

@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?
@T.J.Crowder See updated post.
Yes, that fixes it. Beware of browser support for Object.entries, it's an ES2017 thing.
@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.
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])

}

2 Comments

There is zero need for jQuery here. It's 2016. Arrays have had forEach for several years.
arrays do, but objects do not support forEach

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.