将子对象数组推送到父数组中

时间:2017-03-06 10:40:59

标签: javascript arrays loops

我有一个父数组content,并在lines = []中包含许多对象。在我的节点应用程序中逐行读取数据。 每当找到lines.push(data);斜杠字符时,我就会分割每一行。然后将行更改为对象\和属性data = {}等。现在,当找到具有多个data.marker; data.value; data.children斜杠字符的行时,我希望\是子对象数组。 这是data.children

之后的行数据
split()

这是我的代码转换为 [ 'ft Some older versions read, ', 'fqa And we are writing these things to you so that your joy will be complete. ', 'fqb ', 'f*' ] 数组

data.children

现在当我检查 data.children = []; //object for child var obj = {}; for (var j=0; j<childArr.length; j++) { obj.marker = childArr[j].split(" ")[0] ; obj.value = childArr[j].substr(childArr[j].indexOf(' ')+1) ; } data.children.push(obj); 这就是我得到的

console.log(data.children)

而不是我需要的是

    [ { marker: 'f*', value: 'f*' }]

我确信在将数据推送到子数组时会有些麻烦。任何帮助将不胜感激!提前谢谢。

5 个答案:

答案 0 :(得分:1)

这是我需要的正确答案。很抱歉最近发布它并感谢您的所有努力。谢谢。

data.children = [];

var childArr = [ 'ft Some older versions read, ',
      'fqa And we are writing these things to you so that your joy will be complete. ',
      'fqb  ',
      'f*' ];

var data = {};
data.children = [];
for (var j = 0; j < childArr.length; j++) {
  let split = childArr[j].split(" ");
  data.children.push({
     marker: split[0],
     value: ((split[1] == "" ||split[1] == undefined) ? null : split.slice(1).join(" "))
  });
}
console.log(data.children);

答案 1 :(得分:0)

如果我理解你正确你只是创建一个对象,然后更新它,你想在object.children中为数组“childArr”中的每个项目提供一个对象......

data.children = [];
//object for child
var obj = {};
for (var j=0; j<childArr.length; j++) {
    var obj = {
        marker: childArr[j].split(" ")[0],
        value: childArr[j].substr(childArr[j].indexOf(' ')+1) 
    };
    data.children.push(obj);                  
}

或者我错过了什么?

答案 2 :(得分:0)

&#13;
&#13;
var childArr = [ 'ft Some older versions read, ',
      'fqa And we are writing these things to you so that your joy will be complete. ',
      'fqb  ',
      'f*' ];
    var data = {};
    data.children = [];

    for (var j=0; j<childArr.length; j++) {
        var dataArr = childArr[j].split(" ");
        var obj = {};
        obj.marker = dataArr[0];
        obj.value = (dataArr.length > 1) ? childArr[j].substr(childArr[j].indexOf(' ')+1) : "";
        data.children.push(obj);              
    }

    console.log(data.children);
&#13;
&#13;
&#13;

试试这段代码。它的工作

否则您可以签入JS Fiddle

答案 3 :(得分:0)

尝试使用Array#forEach迭代所有数组元素。找到第一个空格的索引,以分隔新对象的键和值。

&#13;
&#13;
   var array =     [ 'ft Some older versions read, ',
      'fqa And we are writing these things to you so that your joy will be complete. ',
      'fqb  ',
      'f*' ];
   var finalArray =[] ;
   array.forEach(function(value,index){
   var firstSpace = value.indexOf(' ');
   if(firstSpace >=0){
   var key = value.substr(0,firstSpace);
   var val = value.substr(firstSpace+1);
   }
   else{
   var key = value;
   var val = '';
   }
   var obj = {};
   obj.marker = key;
   obj.value = val;
   finalArray.push(obj);
  
   });
  console.log(finalArray);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

var childArr = ['ft Some older versions read, ',
    'fqa And we are writing these things to you so that your joy will be complete. ',
    'fqb  ',
    'f*'
  ],
  data = {
    children: []
  };
for (var j = 0; j < childArr.length; j++) {
  let split = childArr[j].split(" ");
  data.children.push({
    marker: split[0],
    value: split.slice(1, -1).join(" ")
  });
}

console.log(data.children);