Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
After a mysql query, I used the json_encode to convert the result of the query and I'm getting the following:
[
{"id":"1","map_id":"1","description":"This is Athens","lat":"37.77994127700315","lng":"23.665237426757812","title":"Athens"},
{"id":"2","map_id":"1","description":"This is Rome","lat":"41.9100711","lng":"12.5359979","title":"Rome"}
]
I want to convert this to JavaScript array but getting only the values. For example:
myArray = [
[1, 1, 'This is Athens', 37.77994127700315,23.665237426757812, 'Athens']
[2, 1, 'This is Rome', 41.9100711, 12.5359979, 'Rome']
]
I tried many solutions I found here, but I didn't found any solution to give me exactly an array like myArray.
Assuming :
var a = [{"id":"1","map_id":"1","description":"This is Athens","lat":"37.77994127700315","lng":"23.665237426757812","title":"Athens"},{"id":"2","map_id":"1","description":"This is Rome","lat":"41.9100711","lng":"12.5359979","title":"Rome"}];
You can use Array.prototype.map():
var myArray = a.map(function(e){
return [e.id, e.map_id, e.description, e.lat, e.lng, e.title];
});
Result:
[
["1","1","This is Athens","37.77994127700315","23.665237426757812","Athens"],
["2","1","This is Rome","41.9100711","12.5359979","Rome"]
]
Related
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 2 years ago.
Improve this question
I received data from another API. But I don't know how to convert and use data.
data example
{
...
answer:'List(value1,value2,value3,value4)',
...
}
I want to iterate all answer nodes. please help.
Approach
You could capture the group between List(...) and split that by comma ,
Regex test: https://regex101.com/r/xFf39R/1
const answer = 'List(value1,value2,value3,value4)'
const res = /List\((.*)\)/.exec(answer)[1].split(',')
console.log(res)
Reference
RegExp.prototype.exec()
Return value
[...] The returned array has the matched text as the first item, and then one item for each parenthetical capture group of the matched text.
Reading your data example... It seems like the answer is carrying a String as the single quote is presented in your data example.
answer:'List(value1,value2,value3,value4)'
^ ^
And the List(value1,value2,value3,value4) actually looks like some python List in system print.
Well, but this does not help in your direct question.
Assuming your want to get all four values into an array in javascript, do the followings
let data_example = {
...
'answer':'List(value1,value2,value3,value4)',
...
}
let answer_string = data_example['answer'].slice(5,-1)
let your_list = answer_string.split(',')
console.log(your_list)
//["value1","value2","value3","value4"]
But be careful... I assume your List() always start with 'List(' and end with ')'
See more on slice and split
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'd like to updated selected rows in my table. Every row has an id (id is from the table in the database). So I try to add these ids to the serialized data. The array looks like this:
["1", "3"]
formData:
{"f_name":["tom","peter"],"l_name":["fel", "dan"]}
and I'd like to receive this:
{"id":["1","3"],"f_name":["tom","peter"],"l_name":["fel", "dan"]}
How can I achive this?
Demo: https://jsfiddle.net/t6xkbdo0/
You can reduce the elements to build the desired output, finally you have to send the js object as a body in the request.
let formData = Array.from(document.querySelectorAll('input')).reduce((a, i) => {
if (!i.disabled) {
(a[i.name] || (a[i.name] = [])).push(i.value);
}
return a;
}, {id: ['2', '3']});
console.log(formData);
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 3 years ago.
Improve this question
I have this array [ABC, QWE, XYZ]
I would like to turn it into ['ABC', 'QWE', 'XYZ']
When I try to manipulate values in the current array I get: ReferenceError: ABC is not defined
Any ideas on how should I do it?
Thanks!
Convert arrays element types:
Number to strings
var strArr = [1,2,3,4,5].map(String);
// Result: ["1","2","3","4","5"]
We can't do that directly but after little bit change you can do that...
So the current array you said like array [ABC, QWE, XYZ],
Lets design you keys in object first:
var obj = {
ABC:1, QWE:'somevalue', XYZ:new Date()
}
So I created object obj having your variables lets say the three variables, now lets convert:
var arr = [];
for (var key in obj){
console.log(key, obj[key]);
arr.push(String(key));
}
console.log(arr);// you will see the desire result.
Running example here : example
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to pass my string below
{"1":{"11":{"cityid":1,"bpid":11,"name":"Golden Mile
Tower","time":"+00:00","sbpid":50043}},"2":{"34":{"cityid":2,"bpid":34,"name":"KL
Sentral","time":"+00:00","sbpid":50040}},"8":{"244":{"cityid":8,"bpid":244,"name":"Taman
Sri Puteri","time":"+00:00","sbpid":50042}}}
console.log(JSON.parse(str))
It doesn't become what I expect like below
Any idea why??
It woun't produce correct array, because you don't have it.
You have Object not Array
This is array, cause it starts from index 1:
{
"1":{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},
"2":{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},
"8":{"244":{"cityid":8,"bpid":244,"name":"Taman Sri Puteri","time":"+00:00","sbpid":50042}}
}
and this is array :
[
{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},
{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},
{"244":{"cityid":8,"bpid":244,"name":"Taman Sri Puteri","time":"+00:00","sbpid":50042}}
]
For converting Object to Array use loop, like :
var arr = [];
for( var a in your_object ){
arr.push(a);
}
JSON.parse will parse the string only so you are providing the object so it will not parse the object . so wrap your object with single quotes .
I have added the single quote in start and last . now you can check . it will return array .
JSON.parse('{"1":{"11":{"cityid":1,"bpid":11,"name":"Golden Mile Tower","time":"+00:00","sbpid":50043}},"2":{"34":{"cityid":2,"bpid":34,"name":"KL Sentral","time":"+00:00","sbpid":50040}},"8":{"244":{"cityid":8,"bpid":244,"name":"Taman Sri Puteri","time":"+00:00","sbpid":50042}}}');
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have an array like this one:
var data1 = [{ value: 'Afghan afghani', lubID: 'AFN' },{ value: 'Albanian lek', lubID: 'ALL' }];
What i want to do is to dynamically add items to data1 after it has been created. How can i do that?
Thank you.
Just like you push to any other array:
data1.push( { value: 'Something', lubID: 'Something Else'} );
If you're starting from an empty array, just define it first:
var data1 = [];
then start adding values using the push method I've shown you above.