简体   繁体   English

根据另一个对象对对象进行排序

[英]Sort object based on another object

I have following array generated from HTML structure:我有以下从 HTML 结构生成的数组:

const sort = [{ section_id: 'b', children: ['2'] }, { section_id: 'a', children: ['1', '3'] }]

each object in this array represents a section, where children is an array of content ids belonging to that section.此数组中的每个对象代表一个部分,其中 children 是属于该部分的内容 ID 数组。

There is also a product object:还有一个产品对象:

const product = {
            id: 'asd',
            title: 'product title',
            sections: [
                { id: 'a', title: 'section with id a', contents: [{ id: '1' }] },
                { id: 'b', title: 'section with id b', contents: [{ id: '2' }, { id: '3' }] }
            ]
}

I would like to "re-create" product object based on data coming from sort, so the result in this example would be like so:我想根据来自 sort 的数据“重新创建”产品对象,因此本示例中的结果如下所示:

const sortedProduct = {
            id: 'asd',
            title: 'product title',
            sections: [
                { id: 'b', title: 'section with id b', contents: [{ id: '2' }] },
                { id: 'a', title: 'section with id a', contents: [{ id: '1' }, { id: '3' }] }
            ]
}

I have wrote following function to accomplish just that but first section contains all contents in correct order, rest is just repeat of first one.我已经编写了以下函数来完成该功能,但第一部分按正确顺序包含所有内容,其余部分只是重复第一部分。 Please point me to what i'm doing wrong.请指出我做错了什么。

const SrtProduct = () => {
    const sort = serializeSections() // returns array as in example
    let sortedProduct = {}
    sortedProduct.sections = []
    const emptySection = {
        _id: '',
        contents: [],
        title: ''
    }
    Object.keys($productStore).forEach(key => {
        if(key !== 'sections') sortedProduct[key] = product[key]
    })

    sort.forEach((section, index) => {
        sortedProduct.sections[index] = emptySection
        let pozycja = getIndex(product.sections, section.id)
        Object.keys(product.sections[index]).forEach(key => {
            if(key !== 'contents') sortedProduct.sections[index][key] = product.sections[pozycja][key]
        })

        if(section.children.length > 0) section.children.forEach((content_id, ind) => {
            let poz = getContentIndex(product.sections, content_id)
            let tmp = sortedProduct.sections[index]
            tmp.contents.push(product.sections[poz[0]].contents[poz[1]])
            sortedProduct.sections[index] = tmp
        })
    })

    return sortedProduct
}

Above code returns:以上代码返回:

...
sections: [
                { id: 'a', title: 'section with id a', contents: [{id: '2'}, {id: '1'}, {id: '3'}] },
                { id: 'b', title: 'section with id b', contents: [{id: '2'}, {id: '1'}, {id: '3'}] }
            ]

Helper functions getIndex and getContentIndex both work correctly.辅助函数 getIndex 和 getContentIndex 都可以正常工作。

getIndex returns position in array where section with given id is found. getIndex返回数组中找到具有给定 id 的部分的位置。 ex.前任。 getIndex(product.sections, 'b') would return 1 getIndex(product.sections, 'b') 将返回 1

getContentIndex ex. getContentIndex前。 getContentIndex(product.sections, '2') would return [1,0] (section position 1, content position 0) getContentIndex(product.sections, '2') 将返回 [1,0](部分位置 1,内容位置 0)

Input: product , sort输入:产品排序

Output sortedProduct which is product with rearranged sections and contents according to information from sort输出sortedProduct是根据sort信息重新排列部分和内容的产品

I found some mistakes in your code, section.id will be undefined because you are looping over sort it only have section_id no id , any way I rewritten your code to get the expected out put .我在你的代码中发现了一些错误, section.id将是未定义的,因为你正在循环sort它只有section_id没有id ,无论如何我重写了你的代码以获得预期的输出。

const SrtProduct = () => {
    const sort = [{ section_id: 'b', children: ['2'] }, { section_id: 'a', children: ['1', '3'] }]; // returns array as in example
    const $productStore ={
            id: 'asd',
            title: 'product title',
            sections: [
                { id: 'a', title: 'section with id a', contents: [{ id: '1' }] },
                { id: 'b', title: 'section with id b', contents: [{ id: '2' }, { id: '3' }] }
            ]
};
    let sortedProduct = Object.assign({}, $productStore);
    sortedProduct.sections=[];
    // const emptySection = {
    //     _id: '',
    //     contents: [],
    //     title: ''
    // }

    sort.forEach((section, index) => {
        // sortedProduct.sections[index] = emptySection
        let product_sections_index = $productStore.sections.findIndex(x => x.id ===section.section_id);
        sortedProduct.sections[index] = $productStore.sections[product_sections_index]

        // Object.keys(product.sections[index]).forEach(key => {
        //     if(key !== 'contents') sortedProduct.sections[index][key] = product.sections[pozycja][key]
        // })
        sortedProduct.sections[index].contents = [];
        section.children.forEach((value,index2) => {
            sortedProduct.sections[index].contents[index2] = {id: value};
        })
        // if(section.children.length > 0) section.children.forEach((content_id, ind) => {
        //     let poz = getContentIndex(product.sections, content_id)
        //     let tmp = sortedProduct.sections[index]
        //     tmp.contents.push(product.sections[poz[0]].contents[poz[1]])
        //     sortedProduct.sections[index] = tmp
        // })
    })

    return sortedProduct
};

you can see/run the code here您可以在此处查看/运行代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM