Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

vue中数组数据处理

有两个数组,如下
`
arr1=[

{num:'001',name:'酒店'},
{num:'002',name:'饭馆'}

]
arr2=[

{isAlive:'0',num:'001',id:'aaa'},
{isAlive:'1',num:'001',id:'bbb'},
{isAlive:'1',num:'002',id:'ccc'}

]
`
想得到的目标数组
`
arr=[

{num:'001',name:'酒店',isAlive:'0',id:'aaa'},
{num:'002',name:'饭馆'}

]
`
arr1是地点数组,arr2是设备数组,都是data中的变量,一个地点可以有多个设备,只要有一个设备离线(isAlive:'0'),那么就将该设备的状态及id放到目标数组中,否则只取num和name


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
const arr1=[
    {num:'001',name:'酒店'},
    {num:'002',name:'饭馆'}
];
const arr2=[
    {isAlive:'0',num:'001',id:'aaa'},
    {isAlive:'1',num:'001',id:'bbb'},
    {isAlive:'1',num:'002',id:'ccc'}
];

console.log(arr1.map(v=>{
    var index = arr2.findIndex(e => (e.num === v.num && e.isAlive === '0'));
    if (index !== -1){
        v.isAlive = arr2[index].isAlive;
        v.id = arr2[index].id;
    }
    return v;
}))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...