How can I add items to existing array? [closed] - javascript

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.

Related

add array to serialized form data [closed]

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);

Not able to Parse a JSON [closed]

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))

How to loop over a nested JavaScript object in jQuery? [closed]

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
I am new to Javascript and jQuery. I am trying to create something like this (object) below and read the keys and retrieve the values.
var members = {unit:1,name: ["AA", "AB"],userid:["0001","0002"],
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]};
if 'Unit' == 1 then display name array.
Basically I want loop through the object and retrieve the value.
I think you member should be like this
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
var members = [{unit:1,name: ["AA", "AB"],userid:["0001","0002"]},
{unit:2,name: ["BA", "BB"],userid:["0011","0012"]}];
members.forEach(function(v,i){
if(v.unit==1)
{
console.log(v.name);
}
});
I guess this is want you wanted, an object within an object. Where 'unit' is the key for retrieving the objects within the object.
var members = {
1: {
name: ["test","test"],
userid: ["0001","0002"]},
2: {
name: ["test2", "test2"],
userid: ["0011", "0012"]}
};
console.log(members[1]);

Arrays inside objects [closed]

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 8 years ago.
Improve this question
I'm trying to create an object that inside him have some fields, one of them supposed to be an array.
var ob=
{
name : "asdad"
array ?
}
But I dont know how to.
Can anyone help me?
Thank you!
a value is just a value, whether it be a simple scalar or a complicated looking object.
var obj = {
name: "user name",
stocks: ["AAPL", "GOOG"],
cars: [ {make:"Honda", model: "accord"},
{make:"BMW", model: "525", year: 2014}
]
};
do something like this:
var ob= {
name : "asdad",
myArray : ["value1", "value2", "etc."]
}
It would be helpful for you to go over this MDN doc.

convert json object to javascript array [closed]

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"]
]

Categories