我在下面有一个示例代码:
z = [] //To get all the Id
x = [
{
_id: "12345",
name: "A"
},
{
_id: "67890",
name: "B"
}
]
问题是如何才能获得所有" _id"来自" x"并将其保存到" z"。结果如下:
z = ["12345", "67890"]
任何答案都将不胜感激
答案 0 :(得分:1)
这是一个非常简单的Array#map()
public class FileUtil {
private Logger logger = Logger.getLogger(FileUtil.class);
public List<String> loadDataSet(String fileName) {
List<String> stringsFromFile = new ArrayList<>();
String str = "";
// ClassLoader.getResourceAsStream
try (InputStream is = getClass().getClassLoader().getResourceAsStream(fileName)) {
//System.out.println(is);
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
if (is != null) {
while ((str = reader.readLine()) != null) {
stringsFromFile.add(str);
}
}
} catch (FileNotFoundException e) {
logger.error(e);
} catch (IOException e1) {
logger.error(e1);
}
return stringsFromFile;
}
}
&#13;
答案 1 :(得分:1)
使用 array.map
var x = [
{
"_id": "12345",
"name": "A"
},
{
"_id": "67890",
"name": "B"
}
];
var result = x.map(a => a._id);
console.log(result);