gpt4 book ai didi

javascript - 在 javascript 中对数组使用 .map

转载 作者:行者123 更新时间:2023-11-28 18:20:55 24 4
gpt4 key购买 nike

我是一名 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个数组:typeid属性几何。我想生成一个新数组,它只是 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

最佳答案

要获得您想要的输出,您只需要

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

关于javascript - 在 javascript 中对数组使用 .map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39914079/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com