js. 处理数据问题

数据如下,如何按照attributeValue这一项筛选,筛选出当前选中之前的attributeValue;
假设现在选取的是attributeValue3,应该如何筛选出3之前对应的值,感谢各位

let arr = [
    {
        "id": "d844c954-d6a9-4b2f-8b4e-0a0f32f7fabe",
        "skuCode": "rwere",
        "skuName": "4r3",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/5e8c6ec9-80f4-42cc-8364-76d89263d192.png",
        "uniqueCategoryKey": "11,21,31",
        "attributeName1": "高度",
        "attributeValue1": "20",
        "attributeName2": "花色",
        "attributeValue2": "红",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    },
    {
        "id": "706bbbe3-0880-40c3-b765-5aba54e15599",
        "skuCode": "43432",
        "skuName": "543543",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/159e2945-8df0-484f-9b03-d4b44e68eeb1.png",
        "uniqueCategoryKey": "11,22,31",
        "attributeName1": "高度",
        "attributeValue1": "20",
        "attributeName2": "花色",
        "attributeValue2": "白",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    },
    {
        "id": "be2ee289-6cc9-41a4-a7cf-3feb2e931810",
        "skuCode": "432dd",
        "skuName": "432",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/5e8c6ec9-80f4-42cc-8364-76d89263d192.png",
        "uniqueCategoryKey": "12,21,31",
        "attributeName1": "高度",
        "attributeValue1": "50",
        "attributeName2": "花色",
        "attributeValue2": "红",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    },
    {
        "id": "8c918746-448e-48af-ba11-4845032b437e",
        "skuCode": "vfdfds",
        "skuName": "sv",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/159e2945-8df0-484f-9b03-d4b44e68eeb1.png",
        "uniqueCategoryKey": "12,22,31",
        "attributeName1": "高度",
        "attributeValue1": "50",
        "attributeName2": "花色",
        "attributeValue2": "白",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    },
    {
        "id": "35dd54a9-03ed-4624-bdb4-d6b2ac4fbd5d",
        "skuCode": "re",
        "skuName": "wrewrew",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/5e8c6ec9-80f4-42cc-8364-76d89263d192.png",
        "uniqueCategoryKey": "13,21,31",
        "attributeName1": "高度",
        "attributeValue1": "100",
        "attributeName2": "花色",
        "attributeValue2": "红",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    },
    {
        "id": "fbbe454a-2177-449f-b320-df903e469e04",
        "skuCode": "fds",
        "skuName": "ds",
        "price": "22.00",
        "isShelf": "1",
        "image": "https://lumall.inspures.com/images/product/159e2945-8df0-484f-9b03-d4b44e68eeb1.png",
        "uniqueCategoryKey": "13,22,31",
        "attributeName1": "高度",
        "attributeValue1": "100",
        "attributeName2": "花色",
        "attributeValue2": "白",
        "attributeName3": "高度",
        "attributeValue3": "50cm"
    }
]
阅读 1.4k
3 个回答

如果是按某个属性值来过滤,推荐用lodash这个库来支持。

在Lodash中要过滤出所有 "attributeValue3": "50cm"如下面处理:

_.filter(users, function(o) { return o["attributeValue3"]=="50cm"; });

懒得写算法,用了 Lodash

function pickAttributes(list, max) {
    const names = _(_.range(1, max + 1))
        .flatMap(i => [`attributeName${i}`, `attributeValue${i}`])
        .value();

    return list.map(it => _.pick(it, names));
}

const r = pickAttributes(arr, 3);
console.log(r);

按自己的想法写了个。你把想要的数据结构写下更好,要不理解上有偏差

arr.map(item=>{
    let idx = 3;
    let temp = {};
    while(--idx > 0){
        temp["attributeValue" + idx] = item["attributeValue" + idx];
        temp["attributeName" + idx] = item["attributeName" + idx];
    }
    return temp;
})
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题