gpt4 book ai didi

javascript - 获取一系列连接的项目

转载 作者:行者123 更新时间:2023-11-30 13:50:28 25 4
gpt4 key购买 nike

我有一个类别数组,如:

0: {_id: 1, image: "/static/categories/apartment.png", name: "apartments", properties: Array(2), rootCategoryId: null, …}
1: {_id: 2, image: "/static/categories/car.png", name: "cars", properties: Array(1), rootCategoryId: null, …}
2: {_id: 3, image: "/static/categories/dress.png", name: "cloathes", properties: Array(1), rootCategoryId: null, …}
3: {_id: 4, image: "/static/categories/fridge.png", name: "electronicsAndGadgets", properties: Array(1), rootCategoryId: null, …}
4: {_id: 99, image: "/static/categories/phone.png", name: "smartPhones", properties: Array(0), rootCategoryId: 4, …}
5: {_id: 100, image: "/static/categories/shoes.png", name: "shoes", properties: Array(0), rootCategoryId: 3, …}
6: {_id: 1000, image: null, name: "sneakers", properties: Array(0), rootCategoryId: 100, …}
7: {_id: 1001, image: null, name: "sandals", properties: Array(0), rootCategoryId: 100, …}

换句话说。

Root category (image: '../png', rootCategoryId: null)

Sub categories (image: '../png' OR null, rootCategoryId: ID)


1 (root) -> 100 (sub1) -> 1000 (sub2)

由于根总有图片,有时子类没有图片,我想逆向搜索直到找到图片。创建此函数以获取相关类别,但我认为它可以做得更好:

    export const getCategoryImageByByCategoryId = id => {
if (!store) return null;
const getCategory = (id) => _.find(categoriesList, category => category._id === id);
const state = store.getState();
const categoriesList = state.categories.categoriestList;
let currentCategory = getCategory(id);
const result = [currentCategory];
while (currentCategory !== null) {
const nextId = currentCategory.rootCategoryId;
if (!nextId) {
currentCategory = null;
} else {
currentCategory = getCategory(nextId);
result.push(currentCategory);
}
}
return result;
};

最好的方法是什么?应该使用 reduce fn 吗?

最佳答案

您可以创建一个以 _id 为键的对象并调用该函数,直到获得图像。

function getCategoryImageByByCategoryId(id) {
return reference[id].image
|| getCategoryImageByByCategoryId(reference[id].rootCategoryId);
}

var data = [{ _id: 1, image: "/static/categories/apartment.png", name: "apartments", properties: [], rootCategoryId: null }, { _id: 2, image: "/static/categories/car.png", name: "cars", properties: [], rootCategoryId: null }, { _id: 3, image: "/static/categories/dress.png", name: "cloathes", properties: [], rootCategoryId: null }, { _id: 4, image: "/static/categories/fridge.png", name: "electronicsAndGadgets", properties: [], rootCategoryId: null }, { _id: 99, image: "/static/categories/phone.png", name: "smartPhones", properties: [], rootCategoryId: 4 }, { _id: 100, image: null, name: "shoes", properties: [], rootCategoryId: 3 }, { _id: 1000, image: null, name: "sneakers", properties: [], rootCategoryId: 100 }, { _id: 1001, image: null, name: "sandals", properties: [], rootCategoryId: 100 }],
reference = data.reduce((r, o) => {
r[o._id] = o;
return r;
}, {});

console.log(getCategoryImageByByCategoryId(1000));

关于javascript - 获取一系列连接的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com