简体   繁体   English

在javascript中传递值数组和过滤对象

[英]Pass array of values and filtering object in javascript

I would like to know how to filter object by passing arrays of values in javascript 我想知道如何通过在javascript中传递值数组来过滤对象

How to pass array of values as arguments and filter the object using javascript. 如何将值的数组作为参数传递并使用javascript过滤对象。 Based on send and receive with id , get the object value in javascript. 基于id sendreceive ,获取javascript中的对象值。

for id trans, if send (in) is bank and receive (out) is bank , for id fund, if send (in) is credit and receive (out) is bank and for id insta, if send (in) is debit and receive (out) is bank , get result obj 对于id trans,如果send (in)是bankreceive (out)是bank ,对于id基金,如果send (in)是credit并且receive (out)是bank ,对于id insta,如果send (in)是debit并且receive (出)为bank ,获取结果obj

var send=['bank', 'credit', 'debit'];
var receive = ['bank', 'bank', 'bank'];
var id=['trans', 'fund', 'insta'];
var result = getSample(sample,send, receive, id);


I have tried but got stuck
function getSample(sample,sn, rcn, id){
  const temp = sample.map(e => Object.entries(e).map(([k, val]) => val)).flat(3)
    var selectval= temp.filter(x=>x.in==scn && x.out==rcn && x.id == id);
   return selectval;
}

var sample =
  [{
    "btob": [{
      "id": "trans",
      "in": "bank",
      "out": "bank",
      "value": 10,
    },{
      "id": "fund",
      "in": "bank",
      "out": "bank",
      "value": 10
    },{
      "id": "insta",
      "in": "bank",
      "out": "bank",
      "value": 10
    }],
    "ctob": [{
      "id": "trans",
      "in": "credit",
      "out": "bank",
      "value": 20
    },{
      "id": "fund",
      "in": "credit",
      "out": "bank",
      "value": 10
    },{
      "id": "insta",
      "in": "bank",
      "out": "bank",
      "value": 10
    }],
   "dtob": [{
      "id": "trans",
      "in": "debit",
      "out": "bank",
      "value": 20
    },{
      "id": "fund",
      "in": "debit",
      "out": "bank",
      "value": 10
    },{
      "id": "insta",
      "in": "debit",
      "out": "bank",
      "value": 10
    }]
}]

Expected Output:
  result=[
    {
      "id": "trans",
      "in": "bank",
      "out": "bank",
      "value": 10,
    },
    {
      "id": "fund",
      "in": "credit",
      "out": "bank",
      "value": 10
    },
    {
      "id": "insta",
      "in": "debit",
      "out": "bank",
      "value": 10
    }
  ]



Just declare arrays id,send,receive and sample, Then run the below code. 只需声明数组ID,发送,接收和采样,然后运行以下代码即可。

const temp = sample.map(e => Object.entries(e).map(([k, val]) => val)).flat(3);
var jointArray = id.map((currentValue, index) => {
  return id[index]+"-"+send[index]+"-"+receive[index];
});
var result = temp.filter(function(item) { 
   return ( jointArray.indexOf(item.id+"-"+item.in+"-"+item.out)!=-1  ) 
});
console.log(result);  

here is js fiddle https://jsfiddle.net/qcxntpfy/ 这是js小提琴https://jsfiddle.net/qcxntpfy/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM