How can I pass associative array in the below code. I dont wish to create a variable
uiDialog([MyURL,null,'GET'],false);
Some thing like this
uiDialog([url:MyURL,data:null,method:'GET'],false);
I know I can do something like
var arr = new Array(5);
arr["000"]="Rose";
arr["4"]="Ltd";
And pass this array but I am not interested in that I want a one line code
UPDATE
It seams there is no one line solution but if object is not a problem i.e u cant use array function or length you can try this answer Pass associative array to Javascript function
uiDialog({url:MyURL,data:null,method:'GET'},false);
var arr = new Array(3);
arr['url']=MyURL;
arr['data']=null;
arr['method']='GET';
uiDialog(arr,false);
Related
I'm trying to merge multiple arrays from one object using JavaScript(React-Native), the example of the object is :
{"10419": ["37046", "37047"], "9138": ["32809"]}
The result should be like so:
["37046","37047","32809"]
I need to ignore the object name and end up with only one flat array
I used flat() function but it seems not working as I need.
my try looks like :
var obj = this.state.obj // will contain the object
console.log(obj.flat()) // I know that work only with arrays but I tried it out
Thanks
Using Object.values, you can get the values inside the object. That will be 2d array from your input.
Using Array.prototype.flat, you can make it as 1d array as follows.
const input = {"10419": ["37046", "37047"], "9138": ["32809"]}
const result = Object.values(input).flat();
console.log(result);
Object.values(this.state.obj).flat()
I have an Json array like data {"alphaNumeric":[]}.
Here I just want to push the another array [mentioned below] of objects to this Data with out loop concept.
data{"numeric":[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]}.
I used the below code :
data.alphaNumeric.push(data.numeric);
but the output is :
data{"alphaNumeric":[[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]]}.
Expected :
data{"alphaNumeric":[{"id":"1","alpha":"a"},{"id":"2","alpha":"b"}]}.
Help me to resolve.
One solution may be using the concat method. Which isn't really good as it creates a whole new array.
b.alphaNumeric = b.alphaNumeric.concat(a.numeric);
But there is a much better solution using push. It accepts more than just one element, but unfortunately not as an array. This can be however achieved using its apply method:
b.alphaNumeric.push.apply(b.alphaNumeric, a.numeric);
Also you can write your own method (I call it add) which will do this action for you:
Array.prototype.add = function (array) {
this.push.apply(this, array);
return this;
};
b.alphaNumeric.add(a.numeric);
Use concat()
data.alphaNumeric.concat(data.numeric);
.push() and .pop() are for adding and removing single elements in an array. The return value from .concat() is what you're looking for:
var newArr = oldArr.concat(extraArr);
In a loop, I am trying to merge the contents of two arrays:
var myArray = [{a:"a"},{b:"b"}];//first pass in loop
var myArray = [{c:"c"},{d:"d"}];//second pass in loop
For the result, I would like to have this:
results = [{a:"a"},{b:"b"},{c:"c"},{d:"d"}];
If I do this at each pass:
results.splice(0,0,array[i]);
Then the results array becomes a collection of two arrays, rather than a collection of four objects.
I have tried .concat, but that didn't work.
Is there a way to merge the objects comprising an object array with another object array without using a combination of for() and push()?
There must be something simple I have missed.
Thanks
actually concat is the right way to go
in your example you are defining myarray twice. hopefully this is not the case in your code. when I try
var myArray1 = [{a:"a"},{b:"b"}];
var myArray2 = [{c:"c"},{d:"d"}];
var result = myarray1.concat(myarray2);
This must work. if not then please show us the result of these threee lines in your browser
Moataz has your answer, but you mention a loop so you might be looking for:
var result = [];
var myArray;
for (...) {
myArray = /* getMyArray()? */
result = result.concat(myArray);
}
where myArray is set to new values each time. You could remove one line by having the logic that assigns the array to myArray in the concat expression.
jQuery has the .map() function which takes as input either an array or an object but can only output an array.
It seems there are plenty of times when you want to output something more like an associative array so is there another function in jQuery that can output a JavaScript Object?
(I'm pretty sure I've used something like this in another programming language, possibly Perl.)
You can get the same result by declaring the object first, then building it out in the .map() function instead of returning the data.
This example gets all the checkboxes on the page and creates an object out of their ids and values:
var result = new Object();
$(':checkbox').map(function() {
result[this.id] = this.value;
});
I can't figure out what's going on here... I'm trying to copy an array, and then shuffle and join the copy, but it just isn't cooperating.
I have a JS file and am initializing a new array at the top:
var myArray = [];
On load of this JS file, I am pulling data from the Facebook API and stickin' the results in myArray with some HTML around it.
I then have a function which I call on button click which creates a new variable from myArray and then joins and prints out the text in a div element:
var namesList = myArray;
namesList.join('');
$('#my_div').text(namesList);
It's obviously been simplified, but the premise is the same.
My problem is, the array is not joining like it should. It remains in array form and nothing I've tried has fixed this. I've also tried:
var namesList = myArray.slice();
and
var namesList = myArray.slice(0);
to no avail.
HOWEVER: I can manipulate myArray with no problems in the same function and get the results I need. The problem with that is, I need to be able to shuffle the array each time the function is called and then print it out as text; this is why I am copying it in the first place.
For the life of me I can't figure out why this isn't working. All I need to do here is to create a copy of the variable within the scope of the function I'm calling, shuffle it, join it, and print it.
How do I fix this? OR, is there a better alternate? Many thanks in advance!
The join() method returns a string rather than modifying the array reference - you need to change your code to this:
var namesList = myArray;
namelist = namesList.join('');
$('#my_div').text(namesList);
That's because namesList still is an array. join() returns a string, which you should use:
var namesList = myArray.join('');
$('#my_div').text(namesList);
namesList.join('');
This joins the array then throws the result away. You want
namesList = namesList.join('');