2016-11-09 22 views
2

我檢查了任何相關的帖子,但找不到任何。字符串沒有問題,但我無法弄清楚如何實現它。
我需要將以下字符串轉換爲對象。使用包含鍵和屬性值的字符串創建數組

var a ="Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False"; 

進入

[ 
    { 
    "Integer":1, 
    "Float":2.0 
    }, 
    { 
    "Boolean":true, 
    "Integer":6 
    }, 
    { 
    "Float":3.66, 
    "Boolean":false 
    } 
] 
+0

首先,你的那個第一個代碼塊不是有效的JavaScript。這不是JS中多行字符串的工作原理。 – Cerbrus

+0

var a =「Integer,1 Float,2.0 \ nBoolean,True Integer,6 \ nFloat,3.66 Boolean,False」; – wolfsbane

回答

-1

對不起,我以前的答案。我沒有仔細閱讀這個問題。我從Cerbrus的回答中獲得了一些幫助,但仍然無法與他的水平bravo相匹配。我簡化了它,因爲我只知道簡單。

var obj = {}; 
 
var arr = [] 
 
var a ="Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False"; 
 
var aArray = a.split('\n') 
 
for(var i=0 ; i < aArray.length; i++) 
 
     { 
 
      
 
      lilArray = aArray[i].split(" "); 
 
      lillilArray1 = lilArray[0].split(','); 
 
      lillilArray2 = lilArray[1].split(','); 
 
      lla11  = lillilArray1[0]; 
 
      lla12  = lillilArray1[1]; 
 
      lla21  = lillilArray2[0]; 
 
      lla22  = lillilArray2[1]; 
 
      if(lla11 == "Boolean") 
 
       obj[lla11] = lla12 === 'True'; 
 
      else 
 
       obj[lla11] = Number(lla12); 
 
      if(lla21 == "Boolean") 
 
       obj[lla21] = lla22 === 'True'; 
 
      else 
 
       obj[lla21] = Number(lla22); 
 
      arr.push(obj); 
 
      obj={}; 
 
     } 
 
    console.log(arr);

0

這將這樣的伎倆:

var a = "Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False"; 
 

 
var result = a.split('\n').map(function(item) { // Iterate over all objects 
 
    var row = {}; 
 
    item.split(' ') 
 
    .forEach(function(pair) {    // For each key/value pair in the current object 
 
     var data = pair.split(',');   // Separate the key from the value 
 

 
     if (data[0] === 'Boolean')   // If the current value is a boolean: 
 
     row[data[0]] = data[1] === 'True'; // Get the proper boolean value 
 
     else         // Otherwise: 
 
     row[data[0]] = Number(data[1]); // Get the numeric value. 
 
    }); 
 

 
    return row; 
 
}); 
 

 
console.log(result);

0

你可以使用一個對象爲更好的類型轉換到你所需要的類型,它是輕鬆維護新的類型。

var a = "Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False", 
 
    types = { 
 
     Integer: function (v) { return +v; }, 
 
     Float: function (v) { return +v; }, 
 
     Boolean: function (v) { return { True: true, False: false }[v]; }, 
 
     default: function (v) { return v; } 
 
    }, 
 
    result = a.split('\n').map(function (row) { 
 
     o = {}; 
 
     row.split(' ').forEach(function (item) { 
 
      var data = item.split(','); 
 
      o[data[0]] = (types[data[0]] || types.default)(data[1]); 
 
     }); 
 
     return o; 
 
    }); 
 

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

0

你有可能會做如下爲好;

var s ="Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False", 
 
    a = s.split("\n") 
 
     .map(o => o.match(/([a-zA-Z]+),([\w\.]+)/g) 
 
        .reduce(function(p,c){ 
 
        \t  var pv = c.split(","); 
 
        \t  return Object.assign(p,{[pv[0]]: +pv[1] ? +pv[1] : pv[1] !== "False"}); 
 
          }, {})); 
 
console.log(a);

0

首先你需要分析預期的文本的好辦法。 對於你提供的我已經想出了一個解決方案。

var jsonString = "Integer,1 Float,2.0\nBoolean,True Integer,6\nFloat,3.66 Boolean,False"; 
 
var keyValArray = jsonString.split(/[\n]/g); 
 
// Need to parse the string. 
 
var result = []; 
 
// result object to keep new object. 
 
keyValArray.forEach(function(kv, i, a) { 
 
    let obj = {}; 
 
    kv.split(' ').forEach(function(k) { 
 
    var key = k.split(',')[0]; 
 
    let val = k.split(',')[1]; 
 
    
 
    if(isNaN(val)) { 
 
     val = val.toLowerCase() === "true" ? true : false; 
 
    } else { 
 
     val = Number(val); 
 
    } 
 
    
 
    obj[key] = val; 
 
    }); 
 
    result.push(obj); 
 
}); 
 

 
console.log('result : '); 
 
console.info(result);
所有的

相關問題