求和字符串在JSON数组中的出现并将其存储在单独的JSON数组中

时间:2019-01-03 10:59:08

标签: javascript json

我有一个JSON数组,如下所示:

        <select id="Control1" name="Control1" class="form-control">
            @foreach (SelectListItem option in ViewBag.StateList)
            {
                if (option.Value == Model.Control1.ToString())
                {
                    <option value="@option.Value" selected='selected'>@option.Text</option>
                }
                else
                {
                    <option value="@option.Value">@option.Text</option>
                }
            }
        </select>

我想动态地,单独地计算ptype的计数,如果ptype发生变化,则应该应用该计数。

var teamDetails=[ { "pType" : "Search Engines", "value" : 5}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Search Engines", "value" : 1}, { "pType" : "Business", "value" : 1,}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Internet Services", "value" : 1}, { "pType" : "Search Engines", "value" : 6}, { "pType" : "Search Engines", "value" : 1} ];

Expected output:

5 个答案:

答案 0 :(得分:1)

您可以使用reduce,然后使用map方法来首先将对象创建为groupBy,然后获取对象数组。

var data=[ { "pType" : "Search Engines", "value" : 5},{ "pType" : "Content Server", "value" : 1},{ "pType" : "Content Server", "value" : 1},{ "pType" : "Search Engines", "value" : 1},{ "pType" : "Business", "value" : 1,},{ "pType" : "Content Server", "value" : 1},{ "pType" : "Internet Services", "value" : 1},{ "pType" : "Search Engines", "value" : 6},{ "pType" : "Search Engines", "value" : 1} ];

const obj = data.reduce((r, {pType: label, Occurance}) => {
  if(!r[label]) r[label] = {label, Occurance: 1}
  else r[label].Occurance++;
  return r;
}, {})

const result = [].concat(...Object.values(obj).map(({label, Occurance}) => [{label}, {Occurance}])) 
  
console.log(result)

答案 1 :(得分:0)

您可以减少数组,并使用Map来跟踪计数,然后再为其提供所需的样式。

var teamDetails = [{ pType: "Search Engines", value: 5 }, { pType: "Content Server", value: 1 }, { pType: "Content Server", value: 1 }, { pType: "Search Engines", value: 1 }, { pType: "Business", value: 1 }, { pType: "Content Server", value: 1 }, { pType: "Internet Services", value: 1 }, { pType: "Search Engines", value: 6 }, { pType: "Search Engines", value: 1 }];
    output = [];

teamDetails
    .reduce((m, { pType }) => m.set(pType, (m.get(pType) || 0) + 1), new Map)
    .forEach((Occurance, label) => output.push({ label }, { Occurance }));

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 2 :(得分:0)

您可以使用pType计算每个array#reduce的出现,然后使用array#reduceObject.entries创建输出。

var teamDetails=[ { "pType" : "Search Engines", "value" : 5}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Search Engines", "value" : 1}, { "pType" : "Business", "value" : 1,}, { "pType" : "Content Server", "value" : 1}, { "pType" : "Internet Services", "value" : 1}, { "pType" : "Search Engines", "value" : 6}, { "pType" : "Search Engines", "value" : 1} ],
    result = Object.entries(teamDetails.reduce((r, {pType}) => {
      r[pType] = (r[pType] || 0) + 1;
      return r;
    },{}))
    .reduce((r,[label,Occurance]) => {
      r.push({label},{Occurance});
      return r;
    }, []);
console.log(result);

答案 3 :(得分:0)

这是解决方案

var teamDetails= [ 
 { "pType" : "Search Engines", "value" : 5},
 { "pType" : "Content Server", "value" : 1},
 { "pType" : "Content Server", "value" : 1},
 { "pType" : "Search Engines", "value" : 1},
 { "pType" : "Business", "value" : 1,},
 { "pType" : "Content Server", "value" : 1},
 { "pType" : "Internet Services", "value" : 1},
 { "pType" : "Search Engines", "value" : 6},
 { "pType" : "Search Engines", "value" : 1} 
];

const temp = teamDetails.reduce((acc, item) => {
  acc[item.pType] ? acc[item.pType]++ : acc[item.pType] = 1;
  return acc;
}, {});

const res = Object.keys(temp).reduce((acc, key) => {
  acc.push({ label: key });
  acc.push({ Occurence: temp[key] })
  return acc;
},[]);

console.log(res);

答案 4 :(得分:0)

您可以像这样使用reduceObject.etries

使用第一个pType->获得每个reduce作为键值对的计数,然后使用Object.entries来获得每个键值作为数组->使用第二个reduce获取最终所需的数组。

const teamDetails = [{ pType: "Search Engines", value: 5 }, { pType: "Content Server", value: 1 }, { pType: "Content Server", value: 1 }, { pType: "Search Engines", value: 1 }, { pType: "Business", value: 1 }, { pType: "Content Server", value: 1 }, { pType: "Internet Services", value: 1 }, { pType: "Search Engines", value: 6 }, { pType: "Search Engines", value: 1 }]
  
const count = Object.entries(teamDetails.reduce((a, {pType}) =>
        (a[pType] = a[pType] + 1 || 1, a), {}))
    .reduce((a, [k, v]) =>
        (a.push({label: k}, { Occurance: v }), a), [])
        
console.log(count)