简体   繁体   English

值未在Angular的键值对中输入

[英]Value Not entering in Key-value pair in Angular

I have a json file: 我有一个json文件:

var records= 
[
    {
        "category" : "chinese",
        "name": "noodles"
    },
    {
        "category" : "chinese",
        "name": "veg"  
    },
    {
        "category" : "Indian",
        "name": "Dal"  
    },
    {
        "category" : "Indian",
        "name": "Rajma"  
    },
    {
        "category" : "chinese",
        "name": "chicken"  
    },
    {
        "category" : "continental",
        "name": "fish"
    },
    {
        "category" : "continental",
        "name": "Veg"
    },
    {
        "category" : "Indian",
        "name": "Paratha" 
    }
]

I am trying to convert this JSON to following format: 我正在尝试将此JSON转换为以下格式:

var newcat = 
{
   "chinese": [
               { "name": "noodles", "category": "chinese" },
               {"name": "veg", "category": "chinese"}, 
               {"name":"chicken", "category": "chinese"}
            ], 
   "indian":  [
               { "name": "Dal", "category": "indian" },
               {"name": "rajma", "category": "indian"}, 
               {"name":"chicken", "category": "indian" }
            ], 
  "continental": [
                 { "name": "fish", "category": "continental" },
                 { "name": "veg", "category": "continental"} 

            ] 
}

The code I am using in angular is : 我在angular中使用的代码是:

var newcat={}
            var arr= {}
            var isPresent= false;
            var cat
            for (let i=0;i< records.length;i++) {
                cat= records[i].category
                for(let item in newcat) {
                    if(cat == item) {
                        isPresent= true
                    }
                    else {
                            isPresent= false
                    }
                }
                if(isPresent== false){
                    newcat[cat]= []
                }
                else {
                    newcat[cat].push(records[i])
                    console.log("===>",newcat)
                }

            }

I am able to build the object except that, all the items of each category are not entering the new object newcat . 我能够构建对象,除了每个类别的所有项目都不会输入新对象newcat

The result should come like this on console.log: 结果应该在console.log上是这样的:

{chinese: Array(3), indian: Array(3), continental: Array(2)}

Instead it is coming like : 相反,它来像:

{chinese: Array(0), indian: Array(1), continental: Array(1)}

I couldn't figure out why all the elements are not entering 我不知道为什么所有要素都没有进入

If you're using Lodash already, use _.groupBy . 如果您已经在使用Lodash,请使用_.groupBy Otherwise, you could write your own groupBy function: 否则,您可以编写自己的groupBy函数:

 var records = [ { "category" : "chinese", "name": "noodles" }, { "category" : "chinese", "name": "veg" }, { "category" : "Indian", "name": "Dal" }, { "category" : "Indian", "name": "Rajma" }, { "category" : "chinese", "name": "chicken" }, { "category" : "continental", "name": "fish" }, { "category" : "continental", "name": "Veg" }, { "category" : "Indian", "name": "Paratha" } ]; function groupBy(array, key) { return array.reduce((groups, value) => { const group = groups[value[key]] || (groups[value[key]] = []); group.push(value); return groups; }, {}); } console.log(groupBy(records, 'category')); 

Alternatively, with a for loop instead of reduce : 或者,使用for循环而不是reduce

function groupBy(array, key) {
  const groups = {};
  for(let i = 0; i < array.length; i++) {
    const value = array[i];
    const valueKey = value[key];
    if(!groups[valueKey]) groups[valueKey] = [];
    groups[valueKey].push(value);
  }
  return groups;
}

EDIT: Fixing your existing code: 编辑:修复您现有的代码:

Your implementation has two issues. 您的实现有两个问题。 First, the for/in loop that sets isPresent doesn't break after it finds a match. 首先,设置isPresentfor/in循环在找到匹配项后不会break Second, the else statement at the end causes the first item in a category to not be pushed because all you do is create the empty category array. 其次,最后的else语句将导致类别中的第一项不被推送,因为您所做的只是创建一个空的类别数组。 Here it is with fixes: 这是修复程序:

 var records = [ { "category" : "chinese", "name": "noodles" }, { "category" : "chinese", "name": "veg" }, { "category" : "Indian", "name": "Dal" }, { "category" : "Indian", "name": "Rajma" }, { "category" : "chinese", "name": "chicken" }, { "category" : "continental", "name": "fish" }, { "category" : "continental", "name": "Veg" }, { "category" : "Indian", "name": "Paratha" } ]; var newcat = {}; var arr = {}; var isPresent = false; var cat; for (let i=0; i < records.length; i++) { cat= records[i].category for(let item in newcat) { if(cat == item) { isPresent= true break; } else { isPresent= false } } if(isPresent== false){ newcat[cat] = [] } newcat[cat].push(records[i]) } console.log(newcat); 

If you are using ecmascript-6 as tagged, you should have access to the spread operator without the heaviness of using Lodash: 如果您使用的是带有标记的ecmascript-6,则应该可以使用散布运算符,而无需使用Lodash:

This should essentially do all of the work for you: 从本质上讲,这应该为您完成所有工作:

records.map(item => {
    newcat[item.category.toLowerCase()] = newcat[item.category.toLowerCase()] || []
    newcat[item.category.toLowerCase()] = [
        ...newcat[item.category.toLowerCase()],
        { ...item }
    ] 
})

Working snippet below: 工作片段如下:

 var records = [{ "category": "chinese", "name": "noodles" }, { "category": "chinese", "name": "veg" }, { "category": "Indian", "name": "Dal" }, { "category": "Indian", "name": "Rajma" }, { "category": "chinese", "name": "chicken" }, { "category": "continental", "name": "fish" }, { "category": "continental", "name": "Veg" }, { "category": "Indian", "name": "Paratha" } ] var newcat = {} records.map(item => { newcat[item.category.toLowerCase()] = newcat[item.category.toLowerCase()] || [] newcat[item.category.toLowerCase()] = [ ...newcat[item.category.toLowerCase()], { ...item } ] }) console.log(newcat) 

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

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