简体   繁体   English

如何从基于特定子节点的传入 JSON 创建新的数据结构

[英]How to create a new Data Structure from incoming JSON based on specific child Nodes

I have an incoming Json list that looks like the below我有一个传入的 Json 列表,如下所示

[{
    "id": 190,
    "title": "This is a Story title",
    "category": [{
        "id": 43,
        "title": "XXX",
        "titleFr": "YYY"
    }, {
        "id": 27,
        "title": "AAA",
        "titleFr": "BBB"
    }]
}, {
    "id": 191,
    "title": "This is a Story title 2",
    "category": [{
        "id": 43,
        "title": "XXX",
        "titleFr": "YYY"
    }]
}]

I would like to be able to find all the Unique Category Ids, then create a new Data Structure (array) by Grouping by the Category...ie我希望能够找到所有唯一的类别 ID,然后通过按类别分组来创建一个新的数据结构(数组)...即

Category 27- AAA类别 27- AAA

This is a Story title这是一个故事标题

Category 43 - XXX类别 43 - XXX

This is a Story title这是一个故事标题

This is a Story title 2这是一个故事标题 2

I have the below code, which I can use so far to traverse and get the Categories我有以下代码,到目前为止我可以使用它来遍历并获取类别

$.each(data, function (i, ob) {
    $.each(ob, function (ind, obj) {
        if (ind === "category") {
            Categories.push(this);
        }
    });
});
console.log(Categories);

I feel like the above would be highly inefficient, plus it doesn't account for if the Category already exists.我觉得上面的方法效率很低,而且它不考虑类别是否已经存在。

Secondly, I think I would then need to go back over the entire list again, look for the Category.Id, create a new obj when found, then add to a new list?其次,我想我需要再次返回整个列表,查找 Category.Id,找到后创建一个新的 obj,然后添加到新列表中?

I feel like there's a lot of looping here and using $.each would be very inefficient.我觉得这里有很多循环,使用 $.each 会非常低效。

Any help would be appreciated.任何帮助,将不胜感激。

You can achieve this with vanilla JavaScript, building a list of categories using Array.reduce on your input data, iterating over each of the categories for each story and pushing the story titles into an array in the category entry.您可以使用 vanilla JavaScript 实现这一点,在输入数据上使用Array.reduce构建类别列表,迭代每个故事的每个类别并将故事标题推送到类别条目中的数组中。 You can then use Object.values to convert the result object into an array:然后,您可以使用Object.values将结果对象转换为数组:

 const data = [{ "id": 190, "title": "This is a Story title", "category": [{ "id": 43, "title": "XXX", "titleFr": "YYY" }, { "id": 27, "title": "AAA", "titleFr": "BBB" }] }, { "id": 191, "title": "This is a Story title 2", "category": [{ "id": 43, "title": "XXX", "titleFr": "YYY" }] }]; const categories = Object.values(data.reduce((cats, v) => { v.category.forEach(c => { cats[c.id] = cats[c.id] || { id : c.id, title : c.title, titleFr : c.titleFr, stories : [] }; cats[c.id].stories.push(v.title); }); return cats; }, {})); console.log(categories);

暂无
暂无

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

相关问题 如何从JSON数据创建树形结构 - How to create a tree structure from JSON data 如何基于不同页面上的JSON数据填充(特定模板的)节点,并且仍然进行过滤 - How to populate nodes(of a specific template) based on JSON data on different pages, and still Filter 如何在基于 JSON 结构的复选框中显示内部子布尔值? - How to display inner child Boolean values a checkbox based on JSON structure? 如何创建自定义JSON数据结构? - How to create custom JSON data structure? 如何在 Angular 中的自定义结构中创建具有自定义结构的 JSON 数据 - How to create JSON data with custom structure in a custom structure in Angular 如何基于 dom 节点列表创建包含 json 对象数组的文件 - How to create a file with an array of json objects based on a list of dom nodes 从 javascript 中的两个不同 arrays 创建具有特定结构的 Json - Create Json with specific structure from two different arrays in javascript 创建 select 列表,该列表仅显示基于来自 JSON 数据的用户 ID 的特定数据 - Create select list that only show specific data based on user ID from JSON data 如何使用jQuery树结构获取JSON数据的子级 - How to fetch the child of JSON data using jquery tree structure 如何从嵌套数据结构中检索基于谓词/条件的特定属性值的平面数组? - How do I retrieve from a nested data structure a flat array of specific property values based on a predicate / condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM