在JavaScript我想,以檢查是否值myPointID從b是一個。
我試過了:
console.log(a.points.filter((f) => f.myPoint.myPointID === b.myPointID));
我似乎無法找出正確的方法。
var a = [
{
"id": "1",
"name": "sample1",
"points": [
{
"pointID": "12",
"name": "sample point",
"myPoint": {
"myPointID": "12345",
"name": "sample my point",
"form": "form1"
}
}
]
},
{
"id": "2",
"name": "sample2",
"points": [
{
"pointID": "123",
"name": "sample point2",
"myPoint": {
"myPointID": "123456",
"name": "sample my point2",
"form": "form2"
}
}
]
},
{
"id": "3",
"name": "sample3",
"points": [
{
"pointID": "123",
"name": "sample point2",
"myPoint": {
"myPointID": "123456",
"name": "sample my point2",
"form": "form2"
}
}
]
}
];
var b = [
{myPointID: "123456", desc: "sm"},
{myPointID: "123456", desc: "sm2"}
];
uj5u.com熱心網友回復:
您可以從過濾器陣列中獲取想要的 id 并使用嵌套回圈檢查資料。
const
data = [{ id: "1", name: "sample1", points: [{ pointID: "12", name: "sample point", myPoint: { myPointID: "12345", name: "sample my point", form: "form1" } }] }, { id: "2", name: "sample2", points: [{ pointID: "123", name: "sample point2", myPoint: { myPointID: "123456", name: "sample my point2", form: "form2" } }] }, { id: "3", name: "sample3", points: [{ pointID: "123", name: "sample point2", myPoint: { myPointID: "123456", name: "sample my point2", form: "form2" } }] }],
filter = [{ myPointID: "123456", desc: "sm" }, { myPointID: "123456", desc: "sm2" }],
ids = new Set(filter.map(({ myPointID }) => myPointID));
console.log(data.filter(({ points }) =>
points.some(({ myPoint: { myPointID } }) => ids.has(myPointID))
));
.as-console-wrapper { max-height: 100% !important; top: 0; }
uj5u.com熱心網友回復:
對于使用Array.prototype.reduce() 時是否存在任何myPointIDfrom ,這將回傳 true 或 falseba
let b_myPointIds = b.map(function(x){ return x.myPointID})
let reducer = function(accumulator, aChild){ // 'value' is an array item from a
if (accumulator) { // if prev iteration returned true then shortcircuit
return accumulator;
}
for(let i = 0; i<aChild.points.length; i){
if(b_myPointIds.includes(aChild.points[i].myPoint.myPointID)){
return true;
}
}
return false;
}
let isMyPointIdInA = a.reduce(reducer, false); // false is initial value of 'isMyPointIdInA'
console.log(isMyPointIdInA);
顯示代碼片段
var a = [
{
"id": "1",
"name": "sample1",
"points": [
{
"pointID": "12",
"name": "sample point",
"myPoint": {
"myPointID": "12345",
"name": "sample my point",
"form": "form1"
}
}
]
},
{
"id": "2",
"name": "sample2",
"points": [
{
"pointID": "123",
"name": "sample point2",
"myPoint": {
"myPointID": "123456",
"name": "sample my point2",
"form": "form2"
}
}
]
},
{
"id": "3",
"name": "sample3",
"points": [
{
"pointID": "123",
"name": "sample point2",
"myPoint": {
"myPointID": "123456",
"name": "sample my point2",
"form": "form2"
}
}
]
}
];
var b = [
{myPointID: "123456", desc: "sm"},
{myPointID: "123456", desc: "sm2"}
];
//--------------------------------------------------------------------------
let b_myPointIds = b.map(function(x){ return x.myPointID})
let reducer = function(accumulator, aChild){ // 'value' is an array item from a
if (accumulator) { // if prev iteration returned true then shortcircuit
return accumulator;
}
for(let i = 0; i<aChild.points.length; i){
if(b_myPointIds.includes(aChild.points[i].myPoint.myPointID)){
return true; // set accumulator to true
}
}
return false;
}
let isMyPointIdInA = a.reduce(reducer, false); // false is initial value of 'isMyPointIdInA'
console.log(isMyPointIdInA);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/365178.html
標籤:javascript json
上一篇:reactjs如何在陣列上應用fitler后保存初始資料
下一篇:在vue方法中構造陣列
