node.js - NodeJs - How to test if a value exists in an array of objects?
问题描述
I have an array of objects and want to know if a specific value already exists inside this array.
let arrTest = []
let arrDt = [
{"attrJSON": "01"},
{"attrJSON": "01"},
{"attrJSON": "02"},
{"attrJSON": "02"},
{"attrJSON": "03"},
{"attrJSON": "04"},
{"attrJSON": "04"},
{"attrJSON": "05"},
{"attrJSON": "05"},
{"attrJSON": "04"}
]
for(let i = 0; i < 10; i++)
{
console.log(arrDt[i].attrJSON) //01 01 02 02 03 04 04 05 05 04
console.log((arrDt[i].attrJSON in arrTest)) //false false false false false false false false false false
if(!(arrDt[i].attrJSON in arrTest)) {
arrTest.push(arrDt[i].attrJSON)
}
}
console.log(arrTest)
I think the previous code will not work because the value to be tested is in an object property, not a value of an array. The goal here is to build an array arrTest
of unique objects, like:
arrTest = [
"01",
"02",
"03",
"04",
"05",
]
How could I achieve this task?
解决方案
推荐阅读
- c++ - 直接给浮点变量赋值十六进制整数和通过指针转换赋值的区别
- python - 用 Python 解析一个非常大的文本文件?
- java - Heroku:如何为非 java 项目安装 openjdk-8-jdk?
- java - 从 Spring 4.2 升级到 Spring 5.0.5 时预见到的问题
- r - ggplot 绘制聚合总和除以列
- windows - 如何在批处理脚本中将 SYMLINK 更改为 SYMLINKD
- c++ - C++ - 如何指定 MBR 的偏移量?
- ssl - Bazel 通过 ssl 缓存
- android - 如何在 android.mk 中使用 annotationProcessor
- c++ - 点投影和扭曲的二维图像之间的不匹配 [opencv]