0

Here's my input

var id = '1,2,3';
var name = 'a,b,c';
var value = 'x,y,z';

How can i construct a array like this

[
{
"id" : "1",
"name" : "a",
"value" : "x",
}
,
{
"id" : "2",
"name" : "b",
"value" : "y",
}
,
{
"id" : "3",
"name" : "c",
"value" : "z",
}
]

I tried like this

var newArray = [];
newArray.push({'id':id,'name':name,'value':value })

But it gives, a single array with comma seperated value.

How can i do this Pls help

Note : I prefer only javascript

6 Answers 6

1

You could iterate the given strings, split them and assign the values to an object in an array.

var id = '1,2,3',
    name = 'a,b,c',
    value = 'x,y,z',
    keys = ['id', 'name', 'value'],
    result = [];

[id, name, value].forEach(function (a, i) {
    a.split(',').forEach(function (b, j) {
        result[j] = result[j] || {};
        result[j][keys[i]] = b;
    });
});

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

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

6 Comments

you are running loop 9 times . wherever others are running 3 times only
where do you get the message, which user agent or browser are you using?
actually am using nodejs, am doing like var medicine_id = req.body.medicine_id, medicine_name = req.body.medicine_name, keys = ['medicine_id', 'medicine_name'], result = [];
just replace id with medicine_id and name .. don't mind that
you need to change this [id, name, value] to [medicine_id, medicine_name] (without quotes for the variables).
|
1

You can use:

var id_split = id.split(',');
var name_split = name.split(',');
var value_split = value.split(',');

var newArray = [];
for(var i = 0; i < id_split.length; i++){
   newArray.push({'id':id_split[i],'name':name_split[i],'value':value_split[i] })
}

This of course only works if the arrays are all the same length

Comments

1

If you know the elements length beforehand

var allIDs= '1,2,3';
var allNames= 'a,b,c';
var allValues= 'x,y,z';
var tmpArray = [];
for(var i=0;i<3;i++)
tmpArray.push(new {id: allIDs.split(',')[i] , name: allNames.split(',')[i], value: allValues.split(',')[i] });

But for a more generic solution, assuming that your comma sepparated string will always match in length

    var allIDs= '1,2,3';
        var allNames= 'a,b,c';
        var allValues= 'x,y,z';
        var tmpArray = [];

   allIDs =  allIDs.split(',');
    allNames =allNames.split(',');
    allValues = allValues.split(',');
        for(var i=0;i<3;i++)
        tmpArray.push(new {id: allIDs[i] , name: allNames[i], value: allValues[i] });

Comments

1

I would do as follows;

var id = '1,2,3'.split(","),
  name = 'a,b,c'.split(","),
 value = 'x,y,z'.split(","),
result = id.map((e,i) => ({id: e, name: name[i], value: value[i]}));
console.log(result);

Comments

0

var id = '1,2,3';
var name = 'a,b,c';
var value = 'x,y,z';

$('.resultsDiv').html(JSON.stringify(yourFun()))

function yourFun() {
  ida = id.split(',');
  namea = name.split(',');
  valuea = value.split(',');

  var returnvar = [];
  for (var i = 0; i < ida.length; i++) {
    returnvar.push({
      "id": ida[i],
      "name": namea[i],
      "value": valuea[i]
    });
  }
  return returnvar;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="resultsDiv"></div>

Comments

0

First, just split the lists into arrays, then you can perform a loop or use something like map/reduce to generate the compiled array.

var id = '1,2,3';
var name = 'a,b,c';
var value = 'x,y,z';

var idArray = id.split(',');
var nameArray = name.split(',');
var valueArray = value.split(',');

var newArray = idArray.map((id,i) => 
  ({ id: id, name: nameArray[i], value: valueArray[i]  })
);

console.log(newArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.