Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
i am trying to convert json array
{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"};
into this kind of js array
var locations = [
[1, 'abc'],
[2, 'pqr'],
[3, 'xyz']
];
I'm assuming you have the elements objects, (and not as Json as you state):
var data = [{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"}];
You can the convert it to a two-dimensional array like this
var output = new Array();
for (var i = 0; i < data.length; i++) {
output[i] = new Array(data[i].id, data[i].name);
}
You can do this like this.
var jsondata=[{"id":"1","name":"abc"},
{"id":"2","name":"pqr"},
{"id":"3","name":"xyz"}];
var arrayObj=$.parseJSON(jsondata);
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I have an array like this
var array = [["Fitim",13],["Faltin Developer",1],["Local",1]]
i want to get an object like this:
var totalUsers= [{ Fitim:13},{Faltin Developer: 1},{Local:1}]
how can i do this
You can make use of a simple map function to achieve this:
const userArray = [
["Fitim", 13],
["Faltin Developer", 1],
["Local", 1]
];
console.log(
userArray.map((user) => ({
[user[0]]: user[1],
}))
);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
There is an array:
var data = data.addRows([
['Nitrogen', 0.78],
['Oxygen', 0.21],
['Other', 0.01]
]);
What does it mean? Arrays in array? What is it ['Nitrogen', 0.78] ? Why [] brackets?
I tried to reproduce this like:
var arr = [];
arr.push({'Oxygen', 0.21});
That is an array of arrays. Otherwise known as a multidimentional array. The [] inside the [] backets indicate a seperate arrays.
Pointy has already answered this in the comments but I will put it here.
arr.push({'Oxygen', 0.21}); is pushing an object to an array, which is denoted by the {} curly braces (However if you did want to push an object to an array the syntax would actually be arr.push({'Oxygen' : 0.21});). In order to push an array to array you would need to do arr.push(['Oxygen', 0.21]);, which is using [] square brackets.
yes it's array within an array; if you want to reproduce it should be like the following
var arr = [];
arr.push(['oxygen',0.21]);
// to see what's inside
console.log(JSON.stringify(arr));
// to check the type
for ( var i = 0 ; i < arr.length ; i++ ){
console.log('array[',i,'] = ', typeof(arr[i]));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can someone please tell me how to compare elements in array with every other element. I mean in array arr = [a,b,c,d];, I would like to compare a with b,c,d , b with a,c,d, etc. And to do that dynamically, no mather of the size of the array.
Try this:
var a=["a","b","c","d"];
for(var i=0;i<a.length;i++){
for(var j=0;j<a.length;j++){
if(i!=j && a[j]===a[i]){
//match, do whatever you want
}
}
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I need give from text [{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]
only opa...
and set it to array like var array = date.split(regex);
that array[0]="opa" and array[1]="opa2".
Please help me.
var a='[{"value":"opa"},{"value":"opa2"},{"value":"opa3"}]';
var b=JSON.parse(a);
c = [];
b.forEach(function(entry) {
c.push(entry.value);
});
alert(c[0]);
You have array of objets, the task is filter and turn this objets into strings.
var array = [{"value":"opa"},{"value":"opa2"},{"value":"o1pa3"}];
array = array.map(function(item){
if(/opa/.test(item.value)){
return item.value;
}
else return null;
}).filter(function(item){ return item; });
console.log(array)
If you want to parse string and convert to JSON object just use
var array = JSON.parse(STRING);
You can play with this demo
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
So here's the case, I have a variable with a string
var text = "This is a string";
Is it possible to take every word, except for the spaces, " ", of this string and put it into a seperate variable? Like for example:
var 1 = "This";
var 2 = "is";
var 3 = "a";
var 4 = "string";
Thanks in advance.
var text = "This is a string";
var splitted = text.split(" ");
console.log(splitted);
Output
[ 'This', 'is', 'a', 'string' ]
You can access the individual elements like this
console.log(splitted[1]); // will print is