Javascript 如何查找和计算嵌套JSON对象中的唯一值?
我有以下JavaScriptJavascript 如何查找和计算嵌套JSON对象中的唯一值?,javascript,Javascript,我有以下JavaScript { "business": [ { "order_contents": [ { "id": 83, "name": "product 1", "price": "1.99", "quantity": 1, "total": "1.99", "ingredients": [], "extras": [] }, { "id": 83,
{
"business": [
{
"order_contents": [
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 85,
"name": "product 3",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 84,
"name": "product 2",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 83,
"name": "product 1",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
},
{
"id": 84,
"name": "product 2",
"price": "1.99",
"quantity": 1,
"total": "1.99",
"ingredients": [],
"extras": []
}
]
}
]
}
我试图完成的是,当订单通过一个函数扫描JSON并创建一个具有每个唯一产品名称的数组时,每次向数量添加1
我尝试过使用for循环,但它循环了很多次,但在每个产品的嵌套对象中都找不到名称和值,它返回为name=0,值是主对象中的单个嵌套对象。看到每个产品都需要唯一的名称。。。您可以将对象推入对象的分组数组中,然后将对象缩减为单个唯一对象
var data={“business”:[{“order_contents”:[{“id”:83,“name”:“product 1”,“price”:“1.99”,“quantity”:1,“total”:“1.99”,“components”:“1.99”,“quantity”:“product 1”,“price”:“1.99”,“quantity”:1,“total”:“1.99”,“components”:[],“extrass”:“[],{“id”:83,“product 1”,“price”:“1.99”,“quantity”:“total”:“1.99”,“components”:“extrasts”[]),{“id”:85,“名称”:“产品3”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”:[],,{“id”:83,“名称”:“产品1”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”:[],{“id”:84,“名称”:“产品2”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”;{“id”:83,“名称”:“产品1”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”:[],{“id”:83,“名称”:“产品1”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”:[],{“id”:83,“名称”:“产品1”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],“额外的”:[],{“id”:83,“名称”:“产品1”,“价格”:“1.99”,“数量”:1,“总数”:“1.99”,“成分”:[],”额外的【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】数量【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【】【;
函数buildData(){
var items=data.business[0]。订单内容,元素=[],组=[];
对于(变量i=0;i ';
}否则{
logger.innerHTML+=message+'
';
}
}
})();
console.log(buildData());
下面这样的函数可以工作。基本上,您可以将数组作为参数传递,并返回一个对象,该对象1)在属性不存在时获取一个新属性(例如产品id),2)在属性存在时添加到项目计数中。下面的函数生成一个输出,如:
{'product 1':10,'product 2':1,'product 3':2}
function getItems(input) {
var arr = input, obj = {};
for (var i = 0; i < arr.length; i++) {
if (!obj[arr[i].name]) {
obj[arr[i].name] = 1;
} else if (obj[arr[i].name]) {
obj[arr[i].name] += 1;
}
}
return obj;
}
// example use
console.log(getItems(order_contents)); // outputs entire object
console.log(getItems(order_contents)['product 1']); // outputs 10
函数getItems(输入){
var arr=input,obj={};
对于(变量i=0;i
不太喜欢重新发明轮子,所以下面是你如何回答问题的方法
//const objectScan=require('object-scan');
const counts=(haystack)=>objectScan(['business[*].order\u contents[*].name']{
filterFn:({value,context})=>{
上下文[值]=(上下文[值]| | 0)+1;
}
})(干草堆,{});
const data={business:[{order_contents:[{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',配料:[],额外:[]},{id:83,名称:[],价格:'1.99',数量:1,总计:'1.99',配料:[],额外:[]},{id:85,名称:'product 3',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:84,名称:'product 2',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:83,名称:'product 1',价格:'1.99',数量:1,总计:'1.99',成分:[],额外:[]},{id:84,名称:'product 2',价格:'1.99',数量:1,总数:'1.99',成分:[],附加:[]};
控制台日志(计数(数据));
//=>{“产品2”:2,“产品1”:10,“产品3”:1}
。作为控制台包装{最大高度:100%!导入