【问题标题】:List to json conversion javascript列表到 json 转换 javascript
【发布时间】:2015-07-07 17:07:30
【问题描述】:

假设我有两个列表

a= ['apple', 'orange', 'banana']
b= ['red', 'orange', 'yellow']

如何将其转换为 JSON 对象,使用第二个列表作为属性名称的指南?

例如,我会定义属性 = ['fruit', 'color']

为了得到

result = [
  {fruit: 'apple', color: 'red'}, 
  {fruit: 'orange', color: 'orange'}, 
  {fruit: 'banana', color: 'yellow'}]

【问题讨论】:

    标签: javascript json list


    【解决方案1】:

    我创建了一个接受 2 个参数,第一个是 array 作为属性,第二个是 arrayarray 作为列表项,如果属性编号大于给定的属性项,它将处理:

     var create = function(attrList, propertyLists) {
      var result = [];
      var aLen = attrList.length;
      var pLen = propertyLists.length;
      
      if (pLen === 0) {
        return result;
      }
    
      var itemLength = propertyLists[0].length;
      if (itemLength === 0) {
        return result;
      }
    
      var i, j, obj, key;
      for (i = 0; i < itemLength; ++i) {
        obj = {};
        for(j = 0; j < aLen; ++j) {
          key = attrList[j];
          if (typeof propertyLists[j] === 'undefined') {
            //
            continue;
          }
          obj[key] = propertyLists[j][i];
        }
        result.push(obj);
      }
    
      return result;
     };
    
    var a = ['apple', 'orange', 'banana'];
    var b= ['red', 'orange', 'yellow'];
    var attrs = ['fruit', 'color'];
    var jsonObj = create(attrs, [a, b]);
    console.log(jsonObj);

    【讨论】:

      【解决方案2】:

      如果您可以使用下划线或 lodash 之类的库(或重新创建此处使用的方法),则可以这样做:

      var attributes = ['fruit', 'color'];
      var fruits = ['apple', 'orange', 'banana'];
      var colors = ['red', 'orange', 'yellow'];
      
      //Combine arrays in to list of pairs 
      //(this would be expanded with each new array of attribute values)
      //ORDER IS IMPORTANT
      var zipped = _.zip(fruits, colors);
      
      //Map the zipped list, returning an object based on the keys. 
      //Remember the order of the arrays in the zip operation 
      //must match the order of the attributes in the attributes list
      var result = _.map(zipped, function(item, index) {
          return _.object(attributes, item);
      });
      
      console.log(result);
      

      【讨论】:

        【解决方案3】:

        假设两个列表的大小相同并且所有内容都匹配,这应该可以工作。但是,如果它们的大小不同,则会损坏。你的数据是什么样的?

        \\given
        a= ['apple', 'orange', 'banana']
        b= ['red', 'orange', 'yellow']
        attributes = ['fruit', 'color']
        
        \\insert this code
        var result = [];
        for(var i = 0; i<a.length; i++){
            result.push({
                attributes[0]:a[i], 
                attributes[1]:b[i]
            });
        }
        
        console.log(result);
        \\  result = [
        \\      {fruit: 'apple', color: 'red'}, 
        \\      {fruit: 'orange', color: 'orange'}, 
        \\      {fruit: 'banana', color: 'yellow'}]
        

        【讨论】:

        • 您应该使用 var/let 语句初始化结果,以避免创建全局。 var result = [];
        猜你喜欢
        • 2021-04-10
        • 2020-06-18
        • 1970-01-01
        • 2019-12-13
        • 2016-03-13
        • 1970-01-01
        • 2021-08-27
        • 2018-12-13
        • 1970-01-01
        相关资源
        最近更新 更多