想请教大家一个js 数组对象 数据处理问题?谢谢大家?

let curr_data_list = [{

warehouse_id: "1",

wg_id: "111"

},{

warehouse_id: "2",

wg_id: "222"

},{

warehouse_id: "3",

wg_id: "333"

},{

warehouse_id: "4",

wg_id: "444"

}]

let wg_ids_arr = [222,55,444,66,77]

我有上面两个数组,我想通过wg_ids_arr 的值,去和curr_data_list做判断,

如果curr_data_list 的wg_id值,存在wg_ids_arr 数组里,当前项就增加一个属性is_selector_val:true 否则 is_selector_val:false

下面就是我想要的数据:

let curr_data_list = [{

warehouse_id: "1",

wg_id: "111",

is_selector_val:false

},{

warehouse_id: "2",

wg_id: "222",

is_selector_val:true

},{

warehouse_id: "3",

wg_id: "333",

is_selector_val:false

},{

warehouse_id: "4",

wg_id: "444",

is_selector_val:true

}]

想请问下,怎么写,谢谢大家


回答:

curr_data_list.map(item=>{

item.is_selector_val = wg_ids_arr.includes(Number(item.wg_id))

return item

})


回答:

  const curr_data_list = [{

warehouse_id: "1",

wg_id: "111"

}, {

warehouse_id: "2",

wg_id: "222"

}, {

warehouse_id: "3",

wg_id: "333"

}, {

warehouse_id: "4",

wg_id: "444"

}]

const wg_ids_obj = [

{

id: 222

},

{

id: 444

}

]

const newData = curr_data_list.map(item => {

const is_selector_val = wg_ids_obj.some(i => i.id === +item.wg_id)

return {

...item,

is_selector_val

}

})


回答:

curr_data_list.map(item=>({

...item,

is_selector_val:wg_ids_arr.map(wia=>wia.target).includes(Number(item.wg_id))

}))

如果wg_ids_arr是对象数组的话target就是你要取值判断的key


回答:

let curr_data_list = [{

warehouse_id: "1",

wg_id: "111",

},{

warehouse_id: "2",

wg_id: "222",

},{

warehouse_id: "3",

wg_id: "333",

},{

warehouse_id: "4",

wg_id: "444",

}]

let wg_ids_arr = [{id:"11"},{id:"111"},{id:"32"},{id:"444"},{id:"654"}]

const result = curr_data_list.filter((item)=>{

item.is_selector_val = false //默认给每个curr_data_list添加一个is_selector_val为false值

wg_ids_arr.filter((ele)=>{

if(item.wg_id == ele.id){ //遍历过滤,给符合配合的添加is_selector_val为ture

item.is_selector_val = true

}

})

})

console.log(curr_data_list)

不知道是否符合你的要求

以上是 想请教大家一个js 数组对象 数据处理问题?谢谢大家? 的全部内容, 来源链接: utcz.com/p/933066.html

回到顶部