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);
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 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 want to populate a nested array.
The first array option_array is created like this
var option_array =[];
if(!option_array.includes(row.option.option_id))
{
option_array .push({
option_id:row.option.option_id,
option_name:row.option.option_name,
max:row.option.max,
min:row.option.min,
value:row.option.value,
price:row.option.price
})
}
And now, I am creating another array option_group
var option_group=[];
if(!option_group.includes(row.option_group_id))
{
option_group.push({
option_group_id:row.option_group_id,
option_group_name:row.option_group_name,
max:row.max,
min:row.min,
option:option_array
})
}
And I want to modify option_group whereas it will add only option:option_array where current row.option_group_id is equal to the option_group.option_group.option_group_id
.includes checks if the array includes a certain item. Since you have an array of objects, you can't check if the array includes row.option_group_id. You can use find instead. Get the object with row.option_group_id from the array. If it exists, update it. Else, add a new object to the array
const found = option_group.find(a => a.option_group_id == row.option_group_id)
if (found) {
found.option = row.option_array; // update the found object
} else {
option_group.push({
option_group_id: row.option_group_id,
option_group_name: row.option_group_name,
....
})
}
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 am getting object from back end and in front end I am parsing JSON object but the result I am getting object object.
Here is my Code.
JSON (data contains the following JSON)
{
"sc_sub":"ab",
"sc_sub1":"abc"
}
var lclObj = JSON.parse(data);
var a = lclObj[0].sc_sub;
I made changes to object as array, Now the Problem is I am sending one by one values as array from back end I am getting two arrays as
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
[{
"sc_sub":"ab",
"sc_sub1":"abc"
}]
How to remove previous array and set new one?
You can use this code:
var useremail = #Html.Raw(Json.Encode(Data))
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.
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"]
]