Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

js对象,怎么根据两个参数去匹配对应的数据

怎么根据两个字段去匹配,比如,val1 = '黄色' val2 = '23' 就匹配list里第一条

list:[
    {
        id:1,
        skuName: "{"颜色":"黄色","尺码":"23"}",
        name:'裤子',
        img:'/2020/01/11/1609929447463906.png'
    },
    {
        id:2,
        skuName: "{"颜色":"黄色","尺码":"24"}",
        name:'裤子',
        img:'/2020/01/11/1609929447463765.png'
    },
    {
        id:3,
        skuName: "{"颜色":"蓝色","尺码":"23"}",
        name:'裤子',
        img:'/2020/01/11/1609929447490906.png'
    },
    {
        id:4,
        skuName: "{"颜色":"蓝色","尺码":"24"}",
        name:'裤子',
        img:'/2020/01/11/1609929447465496.png'
    }
]

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

1 Answer

0 votes
by (71.8m points)

list.find(fun)

fun 就是判断是否合规的方法,入参是当前对象 list[0] list[1]

image.png
![上传中...]()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...