dsoihsnz85757 2016-01-07 23:57
浏览 16

多数组JavaScript [关闭]

Hello who can help me with this? I have a json and I parse it, after that for each item i need to create an array

This is my json:

  0: Object
    title: "title0"
    content: "content0"
    comment: "comment0"
  1: Object
    title: "title1"
    content: "content1"
    comment: "comment1"

I have a function

var mystockinfo = [];

function uploadSomething(data){
  var myjsonparse = JSON.parse(data.div);
  for (var i = 0; i < myjsonparse.length;){
        mystockinfo[i][0] = myjsonparse[i].title;
        mystockinfo[i][1] = myjsonparse[i].content;
        mystockinfo[i][2] = myjsonparse[i].comment;
    i++;
  }
}

console.log(mystockinfo);

I need to have

[ 0 => [title0, content0, comment0], 1 => [title1, content1, comment1]]

Can you please help me with this???

  • 写回答

2条回答 默认 最新

  • doutuoshou8915 2016-01-08 00:04
    关注

    Using .map() is an easy way to loop through the array and transforming it into the nested arrays you need.

    var obj = [{
      title: "title0",
      content: "content0",
      comment: "comment0"
    }, {
      title: "title1",
      content: "content1",
      comment: "comment1"
    }];
    
    var updated = obj.map(function(ind) {
      return [ind.title, ind.content, ind.comment];
    });
    
    console.log(updated);

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了