我想从几个API中获取数据:
var apis = ['http://api1.com/get', 'http://api13.com/get', 'http://api11.com/get', 'http://api11.com/get', 'http://api2.com/get'];
或
var apis = ['http://api1.com/get', 'http://api5.com/get'];
每个API返回我的对象,例如:
{
title: example,
cities: {{'letter' : 'A', 'name': 'Mexico'}, {'letter' : 'B', 'name' : 'New York'}, {'letter': 'C', 'name': 'London'}}
}
这座城市总是有相同的信。
apis.forEach(function(api) {
//get operations...
var cities = api.cities;
});
在这里,我应该将数据分配给数组,以便以后可以比较它们,但是我不知道该怎么做。
在php中我可以:
$array = [];
foreach ($apis as $i => $api) {
//get operations...
foreach ($cities as $city) {
$array[$i][] = $city['letter'];
}
}
$repetitions = call_user_func_array("array_intersect", $array);
我该如何在JavaScript中进行?
像 var arr = [];
一样声明foreach循环外的空数组然后,只要您想在数组中添加元素,请使用 arr.push(city['letter']);