2
var value = [{
    "rowid": "one, two, three"
}, {
    "rowid": "four"
}]

var selected = value.map(function(varId) {
    return varId.rowid.toString();
});
console.log(selected);
console.log(selected.length);

The Output I'm getting is

["one, two, three", "four"]
2

But I'm expecting

["one", "two", "three", "four"]
4

How can this be done?

2
  • 1
    Just a quick note - what you have is an array of objects, not a multi-dimensional array. Commented Mar 8, 2016 at 15:06
  • Not sure about syntax but the logic should be something like: console.log(selected.split(',').length); Commented Mar 8, 2016 at 15:09

4 Answers 4

3

Use String#split() and Array#reduce():

var value = [{ "rowid": "one, two, three" }, { "rowid": "four" }],
    selected = value.reduce(function (r, a) {
        return r.concat(a.rowid.split(', '));
    }, []);

document.write('<pre>' + JSON.stringify(selected, 0, 4) + '</pre>');

Sign up to request clarification or add additional context in comments.

Comments

0

As mentioned in comment it is array of objects not array of arrays anyway try return varId.rowid.toString().split(',');

Comments

0

You will have to split your first string by the comma (', '), afterwards flatten the multidimensional array.

var value = [{
    "rowid": "one, two, three"
}, {
    "rowid": "four"
}];

var selected = value.map(function(varId) {
    return varId.rowid.toString().split(', ');
});    
// [ [ 'one', 'two', 'three' ], [ 'four' ] ]
selected = [].concat.apply([], selected);
// [ 'one', 'two', 'three', 'four' ]
console.log(selected);
console.log(selected.length);

Comments

0

Try this:

var sel = value.map(function(varId) {
    var rowId = varId.rowid.toString();
    var splitted = rowId.split(",");    
    return splitted;
});
var arrayOfStrings = [].concat.apply([], sel);
for (int i = 0; i < arrayOfStrings.length; i++){
   arrayOfStrings[i] = arrayOfStrings[i].trim();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.