在 javascript 中的数组上使用 .map
using .map on an array in javascript
我是一名 R 程序员,认为 javascript 中的数组有点像 R 中的列表。在 R 中,当我想访问列表的元素时,我会使用 lapply()
和 do.call
将函数应用于内部元素,然后将结果放入新的数据结构中。我在 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 ] } }
]
}
因此,data.features
是一个包含 5 个元素的数组,每个元素包含 4 个数组:type
、id
、properties
、geometry
.我想生成一个只有 properties.result
值的新数组。它的结构如下:
newdata = [0, 0, 0, 0, 0]
到目前为止,我尝试了以下方法,但没有得到我想要的结果:
var result = data.features.map(function(i) {
return{
result: i.properties.result
};
});
console.log(result)
有什么办法可以做到这一点吗?最后我的目的是确定 result == 1
的 any
I would like to generate a new array that is just the
properties.result value.
你几乎走对了路
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
你可以使用filter
var hasAny1 = data.features.filter(function(i) {
return i.properties.result == 1;
}).length > 0;
console.log(hasAny1);
或使用some
var hasAny1 = data.features.some(function(i) {
return i.properties.result == 1;
});
console.log(hasAny1);
您应该 return 您希望成为新数组元素的值:
data.features.map(function (x) {
return x.properties.result;
});
要获得您想要的输出,您只需要
var result = data.features.map(function(i) {
return i.properties.result;
});
这将导致数组 [0,0,0,0,0]
。
要确定其中是否有 1 个,您可以使用 Array.some
var areAny1 = result.some(function(x) { return x == 1; });
下面的实例
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);
我是一名 R 程序员,认为 javascript 中的数组有点像 R 中的列表。在 R 中,当我想访问列表的元素时,我会使用 lapply()
和 do.call
将函数应用于内部元素,然后将结果放入新的数据结构中。我在 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 ] } }
]
}
因此,data.features
是一个包含 5 个元素的数组,每个元素包含 4 个数组:type
、id
、properties
、geometry
.我想生成一个只有 properties.result
值的新数组。它的结构如下:
newdata = [0, 0, 0, 0, 0]
到目前为止,我尝试了以下方法,但没有得到我想要的结果:
var result = data.features.map(function(i) {
return{
result: i.properties.result
};
});
console.log(result)
有什么办法可以做到这一点吗?最后我的目的是确定 result == 1
的 any
I would like to generate a new array that is just the properties.result value.
你几乎走对了路
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
你可以使用filter
var hasAny1 = data.features.filter(function(i) {
return i.properties.result == 1;
}).length > 0;
console.log(hasAny1);
或使用some
var hasAny1 = data.features.some(function(i) {
return i.properties.result == 1;
});
console.log(hasAny1);
您应该 return 您希望成为新数组元素的值:
data.features.map(function (x) {
return x.properties.result;
});
要获得您想要的输出,您只需要
var result = data.features.map(function(i) {
return i.properties.result;
});
这将导致数组 [0,0,0,0,0]
。
要确定其中是否有 1 个,您可以使用 Array.some
var areAny1 = result.some(function(x) { return x == 1; });
下面的实例
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);