qq_42218258
你吃西瓜皮
2019-01-30 14:34
采纳率: 100%
浏览 986

js遍历json数据计算,对比另一条json,组成表格

//有两条json数据a和b,如下图:
//a的json中有一个fruit数组和stationery数组(可以为空),里面有名称和数量。
//b的json中对应的类型和特征
//需求:根据a和b,计算水果数量,组成如下表格。
//这种json对象里套数组有点懵,组成表格时,需要取a,b重新组成新的json吗?
谢谢.

var a = [{
                "id": "1548208772510",
                "area": "上海",
                "fruit": [{
                    "fname": "苹果",
                    "fnum": 4
                }],
                "stationery": [],
            },
            {
                "id": "1548208889319",
                "area": "上海",
                "fruit": [],
                "stationery": [{
                        "sname": "钢笔",
                        "snum": 5
                    },
                    {
                        "sname": "毛笔",
                        "snum": 4
                    }
                ]
            },
            {
                "id": "1548208889319",
                "area": "上海",
                "fruit": [{
                    "fname":"橘子",
                    "fnum": 3
                }
                ],
                "stationery": [{
                        "sname": "钢笔",
                        "snum": 1
                    },
                    {
                        "sname": "毛笔",
                        "snum": 9
                    }
                ]
            }
        ];

var b = [
        {
            "id": "1548208889389",
            "type": '水果',
            "name": '苹果',
            "feature": '又圆又甜'
        },
        {
            "id": "1548208889387",
            "type": '水果',
            "name": '橘子',
            "feature": '又圆又酸'
        },
        {
            "id": "1548208889317",
            "type": '文具',
            "name": '毛笔',
            "feature": '流畅'
        },
        {
            "id": "1548208889387",
            "type": '文具',
            "name": '钢笔',
            "feature": '帅气'
        }
        ];

图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • jslang
    天际的海浪 2019-01-30 16:25
    已采纳

    你的b对象中type属性有重名。我改变了一下

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    <script>
    var a = [{
                    "id": "1548208772510",
                    "area": "上海",
                    "fruit": [{
                        "fname": "苹果",
                        "fnum": 4
                    }],
                    "stationery": [],
                },
                {
                    "id": "1548208889319",
                    "area": "上海",
                    "fruit": [],
                    "stationery": [{
                            "sname": "钢笔",
                            "snum": 5
                        },
                        {
                            "sname": "毛笔",
                            "snum": 4
                        }
                    ]
                },
                {
                    "id": "1548208889319",
                    "area": "上海",
                    "fruit": [{
                        "fname":"橘子",
                        "fnum": 3
                    }
                    ],
                    "stationery": [{
                            "sname": "钢笔",
                            "snum": 1
                        },
                        {
                            "sname": "毛笔",
                            "snum": 9
                        }
                    ]
                }
            ];
    
    var b = [
            {
                "id": "1548208889389",
                "type": '水果',
                "name": '苹果',
                "feature": '又圆又甜'
            },
            {
                "id": "1548208889387",
                "type": '水果',
                "name": '橘子',
                "feature": '又圆又酸'
            },
            {
                "id": "1548208889317",
                "type": '文具',
                "name": '毛笔',
                "feature": '流畅'
            },
            {
                "id": "1548208889387",
                "type": '文具',
                "name": '钢笔',
                "feature": '帅气'
            }
        ];
    </script>
    
    <table border="1">
        <tr>
            <th>类型</th>
            <th>名称</th>
            <th>数量</th>
            <th>特征</th>
        </tr>
    <script type="text/javascript">
    var numbers = {};
    a.forEach(function(n1){
        n1.fruit.forEach(function(n2){
            numbers[n2.fname] = (numbers[n2.fname]||0) + n2.fnum;
        });
        n1.stationery.forEach(function(n2){
            numbers[n2.sname] = (numbers[n2.sname]||0) + n2.snum;
        });
    });
    b.forEach(function(obj){
        document.write('<tr><td>'+obj.type+'</td><td>'+obj.name+'</td><td>'+numbers[obj.name]+'</td><td>'+obj.feature+'</td></tr>');
    });
    </script>
    </table>
    
    </html>
    
    
    点赞 评论
  • qq_42218258
    你吃西瓜皮 2019-01-30 16:52
    <!DOCTYPE html>
    <html>
    
        <head>
            <meta charset="UTF-8">
            <title></title>
        </head>
    
        <body>
                hahahahahhahahahahaah
        </body>
        <script>
             let a = [
                {
                    "id": "1548208772510",
                    "area": "上海",
                    "fruit": [
                        {
                            "fname": "苹果",
                            "fnum": 4
                        }
                    ],
                    "stationery": [],
                },
                {
                    "id": "1548208889319",
                    "area": "上海",
                    "fruit": [],
                    "stationery": [
                        {
                            "sname": "钢笔",
                            "snum": 5
                        },
                        {
                            "sname": "毛笔",
                            "snum": 4
                        }
                    ]
                },
                {
                    "id": "1548208889319",
                    "area": "上海",
                    "fruit": [
                        {
                            "fname":"橘子",
                            "fnum": 3
                        }
                    ],
                    "stationery": [
                        {
                            "sname": "钢笔",
                            "snum": 1
                        },
                        {
                            "sname": "毛笔",
                            "snum": 9
                        }
                    ]
                }
            ]; 
            let b = [
                {
                    "id": "1548208889389",
                    "type": '水果',
                    "name": '苹果',
                    "type": '又圆又甜'
                },
                {
                    "id": "1548208889387",
                    "type": '水果',
                    "name": '橘子',
                    "type": '又圆又酸'
                },
                {
                    "id": "1548208889317",
                    "type": '文具',
                    "name": '毛笔',
                    "type": '流畅'
                },
                {
                    "id": "1548208889387",
                    "type": '文具',
                    "name": '钢笔',
                    "type": '帅气'
                }
            ];
            //遍历b,给b的每一项新增number、arr属性
            for(var i = 0; i < b.length;i++){
                b[i].number = 0;
                b[i].arr = [];
            }
    
            for(var i = 0; i < a.length;i++){
                for(var j = 0;j < a[i].fruit.length;j++){
                    //把水果抽出来push到跟b里面一样名字的水果的arr中
                    for(k = 0; k < b.length;k++){
                        if(a[i].fruit[j].fname == b[k].name){
                            b[k].arr.push(a[i].fruit[j]);
                        }
                    }
                }
                for(var j = 0;j < a[i].stationery.length;j++){
                    //把文具抽出来push到跟b里面一样名字的文具的arr中
                    for(k = 0; k < b.length;k++){
                        if(a[i].stationery[j].sname == b[k].name){
                            b[k].arr.push(a[i].stationery[j]);
                        }
                    }
                }
            }
    
            //然后再遍历b计算各自水果的数量
            for(var i = 0;i < b.length;i++){
                //遍历每一项的arr
                var num = 0;
                for(var j = 0; j < b[i].arr.length;j++){
                    if(b[i].arr[j].fnum){
                        num += b[i].arr[j].fnum
                    }
                    if(b[i].arr[j].snum){
                        num += b[i].arr[j].snum
                    }
                }
                b[i].number = num;
            }
            console.log('打印b你就会看到水果和文具全部抽出啦放到b里面去了,并且已经计算好了数量',b);
        </script>
    
    </html>
    
    点赞 评论
  • weixin_39962661
    紫色核桃 2019-01-30 18:07

    <br> var a = [{<br> &quot;id&quot;: &quot;1548208772510&quot;,<br> &quot;area&quot;: &quot;上海&quot;,<br> &quot;fruit&quot;: [{<br> &quot;fname&quot;: &quot;苹果&quot;,<br> &quot;fnum&quot;: 4<br> }],<br> &quot;stationery&quot;: [],<br> },<br> {<br> &quot;id&quot;: &quot;1548208889319&quot;,<br> &quot;area&quot;: &quot;上海&quot;,<br> &quot;fruit&quot;: [],<br> &quot;stationery&quot;: [{<br> &quot;sname&quot;: &quot;钢笔&quot;,<br> &quot;snum&quot;: 5<br> },<br> {<br> &quot;sname&quot;: &quot;毛笔&quot;,<br> &quot;snum&quot;: 4<br> }<br> ]<br> },<br> {<br> &quot;id&quot;: &quot;1548208889329&quot;,<br> &quot;area&quot;: &quot;上海&quot;,<br> &quot;fruit&quot;: [{<br> &quot;fname&quot;:&quot;橘子&quot;,<br> &quot;fnum&quot;: 3<br> }<br> ],<br> &quot;stationery&quot;: [{<br> &quot;sname&quot;: &quot;钢笔&quot;,<br> &quot;snum&quot;: 1<br> },<br> {<br> &quot;sname&quot;: &quot;毛笔&quot;,<br> &quot;snum&quot;: 9<br> }<br> ]<br> }<br> ];<br> var b = [<br> {<br> &quot;id&quot;: &quot;1548208889389&quot;,<br> &quot;type&quot;: &#39;水果&#39;,<br> &quot;name&quot;: &#39;苹果&#39;,<br> &quot;types&quot;: &#39;又圆又甜&#39;<br> },<br> {<br> &quot;id&quot;: &quot;1548208889387&quot;,<br> &quot;type&quot;: &#39;水果&#39;,<br> &quot;name&quot;: &#39;橘子&#39;,<br> &quot;types&quot;: &#39;又圆又酸&#39;<br> },<br> {<br> &quot;id&quot;: &quot;1548208889317&quot;,<br> &quot;type&quot;: &#39;文具&#39;,<br> &quot;name&quot;: &#39;毛笔&#39;,<br> &quot;types&quot;: &#39;流畅&#39;<br> },<br> {<br> &quot;id&quot;: &quot;1548208889387&quot;,<br> &quot;type&quot;: &#39;文具&#39;,<br> &quot;name&quot;: &#39;钢笔&#39;,<br> &quot;types&quot;: &#39;帅气&#39;<br> }<br> ];<br> let tab1=$(&quot;#tb&quot;)<br> let something=&quot;<tr style='background-color:#0066FF;'><td>类型</td><td>名称</td><td>数量</td><td>特征</td></tr>&quot;;<br> for (let i = 0;i<b.length;i++){<br> let model = b[i];<br> console.log(model);<br> let number=0;<br> for (let j=0;j<a.length;j++){<br> let something =a[j];<br> if (something.fruit.length>0){<br> if(something.fruit[0].fname==model.name){<br> number=number+something.fruit[0].fnum;<br> }<br> }<br> console.log(something.stationery.length);<br> let model1=something.stationery;<br> if (model1.length&gt;0){<br> for (let k;k<model1.length;k++){<br> if(model1[k].sname==model.name){<br> number=number+something.stationery[n].snum<br> }<br> }<br> }<br> }<br> let type = model.type;<br> let types = model.types;<br> something+="<tr><td>&quot;+type+&quot;</td><td>&quot;+model.name+&quot;</td><td>&quot;+number+&quot;</td><td>&quot;+types+&quot;</td></tr>&quot;<br> }<br> tab1.append(something)<br>

    点赞 评论

相关推荐