[英]using .map on an array in javascript
I'm an R programmer and think of arrays in javascript kind of like lists in R. In R when I want to access elements of a list I would use lapply()
and do.call
to apply a function to the inner elements and then place the result into a new data structure. 我是R程序员,想到了javascript中类似于R中的列表的数组。在R中,当我想访问列表中的元素时,我将使用
lapply()
和do.call
将函数应用于内部元素,然后将结果放入新的数据结构中。 I read about the map
function in javascript but I can't seems to crack it in my case: 我在javascript中阅读了有关
map
函数的信息,但就我而言似乎无法破解它:
data = {"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "CRS" } },
"features": [
{ "type": "Feature",
"id": 6,
"properties": {"species": "giraffe",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 1, 5] } },
{ "type": "Feature",
"id": 7,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some text" },
"geometry": { "type": "Point", "coordinates": [ 2,3 ] } },
{ "type": "Feature",
"id": 8,
"properties": { "species": "goat",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 1,6 ] } },
{ "type": "Feature",
"id": 16,
"properties": { "species": "giraffe",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 3,4 ] } },
{ "type": "Feature",
"id": 18,
"properties": { "species": "pig",
"result": 0,
"popup_text": "Some Text" },
"geometry": { "type": "Point", "coordinates": [ 2,7 ] } }
]
}
So, data.features
is an array of 5 elements, each of these elements contains 4 arrays: type
, id
, properties
, geometry
. 因此,
data.features
是一个由5个元素组成的数组,每个元素都包含4个数组: type
, id
, properties
和geometry
。 I would like to generate a new array that is just the properties.result
value. 我想生成一个仅是
properties.result
值的新数组。 It would have a structure like this: 它将具有如下结构:
newdata = [0, 0, 0, 0, 0]
So far I tried the following but it doesn't result in what I want: 到目前为止,我尝试了以下操作,但未达到我想要的结果:
var result = data.features.map(function(i) {
return{
result: i.properties.result
};
});
console.log(result)
Any ideas how I can do this? 有什么想法可以做到吗? In the end my purpose is to determine if
any
of the result == 1
最后,我的目的是确定是否有
any
result == 1
To get the output you wanted you just needed 要获得您只需要的输出
var result = data.features.map(function(i) {
return i.properties.result;
});
That will result in an array of [0,0,0,0,0]
. 这将导致
[0,0,0,0,0]
的数组。
To determine if any of them are 1 you could use Array.some
要确定它们是否为1,可以使用
Array.some
var areAny1 = result.some(function(x) { return x == 1; });
Live example below 下面的直播示例
var data = {"type": "FeatureCollection", "crs": { "type": "name", "properties": { "name": "CRS" } }, "features": [ { "type": "Feature", "id": 6, "properties": {"species": "giraffe", "result": 0, "popup_text": "Some text" }, "geometry": { "type": "Point", "coordinates": [ 1, 5] } }, { "type": "Feature", "id": 7, "properties": { "species": "pig", "result": 0, "popup_text": "Some text" }, "geometry": { "type": "Point", "coordinates": [ 2,3 ] } }, { "type": "Feature", "id": 8, "properties": { "species": "goat", "result": 0, "popup_text": "Some Text" }, "geometry": { "type": "Point", "coordinates": [ 1,6 ] } }, { "type": "Feature", "id": 16, "properties": { "species": "giraffe", "result": 0, "popup_text": "Some Text" }, "geometry": { "type": "Point", "coordinates": [ 3,4 ] } }, { "type": "Feature", "id": 18, "properties": { "species": "pig", "result": 0, "popup_text": "Some Text" }, "geometry": { "type": "Point", "coordinates": [ 2,7 ] } } ] }; var result = data.features.map(function(x){ return x.properties.result; }); console.log(result); var areAny1 = result.some(function(x) { return x === 1 }); console.log(areAny1);
I would like to generate a new array that is just the properties.result value.
我想生成一个仅是properties.result值的新数组。
You were almost on the right path 你几乎在正确的道路上
var result = data.features.map(function(i) {
return i.properties.result;
});
console.log(result)
In the end my purpose is to determine if any of the result == 1
最后,我的目的是确定是否有任何结果== 1
you can use filter
你可以使用
filter
var hasAny1 = data.features.filter(function(i) {
return i.properties.result == 1;
}).length > 0;
console.log(hasAny1);
var hasAny1 = data.features.some(function(i) {
return i.properties.result == 1;
});
console.log(hasAny1);
You should just return the value that you want to be an element of the new array: 您应该只返回要成为新数组元素的值:
data.features.map(function (x) {
return x.properties.result;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.