I am wondering how I can return multiple values from an array like this:
var countries = [
{ key: "Spain", doc_count: 1378 },
{ key: "Greece", doc_count: 1259 }
];
This is what I have set up so far and it works fine for returning a single value. I am wondering how I could pass an array of countries though instead of looking for a single country.
var countriesFound = countries.filter(function(country) {
return country.key === 'Spain';
});
On that note I would also like to add the found object to the front of the array so I could have a copy of it inside my countries array.
Using this results in some unexpected results because I end up with an array as my first item in the countries array and my object that I want is stored withint that array.
countries.unshift(countriesFound);
Results in (at least I think it looks like this typed out):
var countries = [
[{ key: "Spain", doc_count: 1378 }],
{ key: "Spain", doc_count: 1378 },
{ key: "Greece", doc_count: 1259 }
];
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…