In javascript I'm trying to check if value myPointID from b is in a.
I've tried:
console.log(a.points.filter((f) => f.myPoint.myPointID === b.myPointID));
I can't seem to figure out the correct way.
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"}
];
CodePudding user response:
You could take the wanted id from the filter array and check the data with a nested loop.
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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
This returns true or false for whether any myPointID
from b
exists in a
using Array.prototype.reduce()
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);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>