将数组数组转换为JSON对象列表而不是JSON字符串 [英] Converting an array of arrays to list of JSON objects instead of JSON strings

查看:84
本文介绍了将数组数组转换为JSON对象列表而不是JSON字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数组数组转换为JSON对象列表。

  var headers = ['first名称','姓氏','年龄'] 

var data = [[''John','Gill','21'],['Sai','Varun','21' ]]

当我们使用以上两个列表生成JSON列表时,输出将像

  [{'First name':'John','Last name':'Gill',age:'21'} ,
{'名字':'Sai','姓氏':'Varun',年龄:'21'}]

但是我需要输出为as,(双引号字符串和JSON不应为字符串,而应为JSON对象)

  [{名字:约翰,姓氏:吉尔,年龄: 21},
{ 名字: Sai,姓氏: Varun,年龄: 21}]

我尝试使用 JSON.stringify ,但是结果不会是 JSON对象,这将是一个 JSON字符串,这将阻止访问e



我经历了很多这样的回答,但徒劳无功。

解决方案

您可以首先使用对象生成一个数组,然后对该数组进行字符串化。 / p>

  var headers = ['first name','last name','age '],数据= [[['John','Gill','21'],['Sai','Varun','21']],结果= data.map(function(a){var object = { }; headers.forEach(function(k,i){object [k] = a [i];}); return object;}); console.log(JSON.stringify(result)); console.log(result);  

  .as-console-wrapper {max-height:100%!important;最高:0; }  



ES6



  var headers = ['first name','last name','age'],data = [[''John' ,'Gill','21'],['Sai','Varun','21']],结果= data.map(a =>标头.reduce((r,k,i)=>对象.assign(r,{[k]:a [i]}),{})); console.log(JSON.stringify(result)); console.log(result);  

  .as-console-wrapper {max-height:100%!important;最高:0; }  


I'm trying to convert an array of arrays to a list of JSON objects.

var headers = ['first name','last name','age']

var data = [ [ 'John', 'Gill', '21' ], [ 'Sai', 'Varun', '21' ] ]

When we use the above two lists to generate a list of JSON's, output will be like,

[ { 'First name': 'John', 'Last name': 'Gill', age: '21' },
  { 'First name': 'Sai', 'Last name': 'Varun', age: '21' } ]

But I'm in need of the output to be as, (double quoted strings and the JSON should not be a string but should be a JSON object)

[ {"First name":"John","Last name":"Gill","age":"21"},
  {"First name":"Sai","Last name":"Varun","age":"21"} ]

I tried using JSON.stringify but the result won't be a JSON object right, it'll be a JSON string which would prevent accessing each of the JSON's from the list as a whole.

I have gone through many such answers but in vain. Can someone shed some light on this?!

解决方案

You could generate an array with the objects first and the stringify the array.

var headers = ['first name', 'last name', 'age'],
    data = [['John', 'Gill', '21'], ['Sai', 'Varun', '21']],
    result = data.map(function (a) {
        var object = {};
        headers.forEach(function (k, i) {
            object[k] = a[i];
        });
        return object;
    });
    
console.log(JSON.stringify(result));
console.log(result);

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

ES6

var headers = ['first name', 'last name', 'age'],
    data = [['John', 'Gill', '21'], ['Sai', 'Varun', '21']],
    result = data.map(a => headers.reduce((r, k, i) => Object.assign(r, { [k]: a[i] }), {}));
    
console.log(JSON.stringify(result));
console.log(result);

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

这篇关于将数组数组转换为JSON对象列表而不是JSON字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆