JS's operation on array
let a = [["1010","20","10"], ["3010","40","20"]]
let b = [
{
KUNN2:"0000000001",
PARVW_TEXT:"",
VKORG:"3010",
VTWEG:"40",
VSBED:"20"
},
{
KUNN2:"0000000002",
PARVW_TEXT:"",
VKORG:"1010",
VTWEG:"20",
VSBED:"10"
},
{
KUNN2:"0000000003",
PARVW_TEXT:"",
VKORG:"4100",
VTWEG:"30",
VSBED:"10"
},
{
KUNN2:"0000000004",
PARVW_TEXT:"",
VKORG:"3010",
VTWEG:"40",
VSBED:"20"
},
{
KUNN2:"0000000005",
PARVW_TEXT:"",
VKORG:"1010",
VTWEG:"20",
VSBED:"10"
}
]
if there are two arrays on both sides above, the array an is a match, and then the result I want is
let c = [{
KUNN2: "0000000002",
PARVW_TEXT: "recipient",
VKORG:"1010",
VTWEG:"20",
VSBED:"10"
}, {
KUNN2: "0000000005",
PARVW_TEXT: "recipient",
VKORG:"1010",
VTWEG:"20",
VSBED:"10"
}], [{
KUNN2: "0000000001",
PARVW_TEXT: "recipient",
VKORG:"3010",
VTWEG:"40",
VSBED:"20"
}, {
KUNN2: "0000000004",
PARVW_TEXT: "cashier",
VKORG:"3010",
VTWEG:"40",
VSBED:"20"
}]
I hope you can give me your advice, but I don"t appreciate it very much. -sharp-sharp-sharp topic description
sources of topics and their own ideas
related codes
/ / Please paste the code text below (do not replace the code with pictures)
what result do you expect? What is the error message actually seen?
1. The first value of the an array you give should be 1010, right? the value of the
2.an array. I can see at first sight that it corresponds to the three values of VKORG,VTWEG,VBED. Is there any different demand? if it is corresponding, you can press the following code
.
let a = [['1010','20','10'], ['3010','40','20']]
let b = [
{
KUNN2:"0000000001",
PARVW_TEXT:"",
VKORG:'3010',
VTWEG:'40',
VSBED:'20'
},
{
KUNN2:"0000000002",
PARVW_TEXT:"",
VKORG:'1010',
VTWEG:'20',
VSBED:'10'
},
{
KUNN2:"0000000003",
PARVW_TEXT:"",
VKORG:'4100',
VTWEG:'30',
VSBED:'10'
},
{
KUNN2:"0000000004",
PARVW_TEXT:"",
VKORG:'3010',
VTWEG:'40',
VSBED:'20'
},
{
KUNN2:"0000000005",
PARVW_TEXT:"",
VKORG:'1010',
VTWEG:'20',
VSBED:'10'
}
]
let c= b.filter((item)=>{
let index = a.find((arr)=>{
return arr[0]=== item.VKORG && arr[1]===item.VTWEG && arr[2]===item.VSBED
})
if(index){
return item
}
})
console.log(c)
the second method is to judge that as long as the value in obj contains the value in the array, it will be returned. From your reply, you can see that the number of subarrays of array an is also uncertain, but the problem will also be returned
if the following data exists.
{
KUNN2:"0000000001",
PARVW_TEXT:"",
VKORG:'10',
VTWEG:'1010',
VSBED:'20'
}
let d = a.map((keys)=>{
let res = b.filter((item)=>{
let objarr = Object.values(item)
let oldlength = objarr.length
//
let newarr = [...objarr,...keys]
let newlength =[...new Set(newarr)].length
if(oldlength === newlength){
return item
}
})
return res
})
should be what you want
let a = [['1010', '20', '10'], ['3010', '40', '20']]
let b = [
{
KUNN2: "0000000001",
PARVW_TEXT: "",
VKORG: '3010',
VTWEG: '40',
VSBED: '20'
},
{
KUNN2: "0000000002",
PARVW_TEXT: "",
VKORG: '1010',
VTWEG: '20',
VSBED: '10'
},
{
KUNN2: "0000000003",
PARVW_TEXT: "",
VKORG: '4100',
VTWEG: '30',
VSBED: '10'
},
{
KUNN2: "0000000004",
PARVW_TEXT: "",
VKORG: '3010',
VTWEG: '40',
VSBED: '20'
},
{
KUNN2: "0000000005",
PARVW_TEXT: "",
VKORG: '1010',
VTWEG: '20',
VSBED: '10'
}
]
const result = [];
for (const aItem of a) {
const bFilter = b.filter(({ VKORG, VTWEG, VSBED }) => VKORG === aItem[0] && VTWEG === aItem[1] && VSBED === aItem[2]);
bFilter.length > 0 && result.push(bFilter);
}
console.log(result);