Reputation: 432
I have a parent array lines = []
and contains a number of objects in lines.push(data);
. The data is read line by line in my node application.
And i split each line whenever \
slash character is found. Then change line into Objects data = {}
and properties data.marker; data.value; data.children
and so. Now when a line which has several \
slash characters is found i want that data.children
to be a child array of objects.
This is the line data after split()
[ 'ft Some older versions read, ',
'fqa And we are writing these things to you so that your joy will be complete. ',
'fqb ',
'f*' ]
and this is my code to convert into data.children
array
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);
Now when i check console.log(data.children)
this is what i gets
[ { marker: 'f*', value: 'f*' }]
instead of what i need is
[ { marker: 'ft' , value: 'Some older versions read,' },
{ marker: 'fqa', value: 'And we are writing these things to you so that your joy will be complete.' },
{ marker: 'fqb', value: 'null' },
{ marker: 'f*', value: 'null' },]
I am sure that it will be some trouble while i am pushing data into children array. Any help will be appreciated! Thanks in advance.
Upvotes: 1
Views: 12649
Reputation: 432
This is the right answer i needed. Sorry for posting it very lately and appreciate all of your efforts. Thank you.
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);
Upvotes: 2
Reputation: 1647
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);
Try this code. Its working
Otherwise you can Check in JS Fiddle
Upvotes: 0
Reputation: 6994
Try using Array#forEach
to iterate over all the array elements.Find the index of first space to separate keys and values of new objects.
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);
Upvotes: 0
Reputation: 13943
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);
Upvotes: 0
Reputation: 8628
If I understand you correctly you are only creating one object, then updating it, wheras you want 1 object in object.children for each item in the array "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);
}
Or did I miss something?
Upvotes: 0