Using index to pull items from an object - javascript

If I have an object with items named and ending in sequential numbers:
var theobject = { item1:, item2:, item3:, ...etc }
this method of extracting the objects and used in a for loop does not seem to work. Should this work provided the rest of the function is correct?
theobject.item+i

You can do something like theobject['item'+i].
But you can do something better using jquery foreach so that you can iterate over keys.

Related

Javascript Get flat array from object contain multiple arrays

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

Javscript: remove curly brackets from objects(with keys and values) in an array

I have an array like this(data retrieved from mySql and json_encode() in PHP, coming back as a json object(totally 19 elements in this array, and all the objects in different order in the element)):
const array=[
[{"name":"jason"},{"age":16},{"location":"London"}],
[{"age":24},{"location":"Tokyo"},{"name":"amy"}]
]
How to convert it to an array like this, removing curly brackets?
const array=[
{"name":"jason","age":16,"location":"London"},
{"name":"amy","age":24,"location":"Tokyo"}
]
I have tried to convert to string, then
String.replace(/[{}]/g, '');
But what's next? I got stuck at converting back to array again.
And the other question is:For an array like this, when to access the keys and values, is it neccesary to re-structure the keys and values to make them the same order in each element, otherwise it doesn't look nice and is not easy to access?
[
[{"name":"jason"},{"age":16},{"location":"London"}],
[{"age":24},{"location":"Tokyo"},{"name":"amy"}]
]
Any tips on how to think about flattening this will be much appreciated!
The .replace() method is used for strings, not objects/arrays. Instead, you can merge the objects within each inner array together by using .map() to trasform each array, and Object.assign() to merge the given array of objects.
See example below:
const array = [
[{"name":"jason"},{"age":16},{"location":"London"}],
[{"age":24},{"location":"Tokyo"},{"name":"amy"}]
];
const res = array.map(inner => Object.assign({}, ...inner));
console.log(res);
The order of your (string) keys in the resulting objects will appear in the order that they're inserted, so as your object order is different for each inner array, your resulting object's key-ordering will also be different once they're merged. However, this shouldn't matter too much as relying on object key ordering is often not the best idea, and can be done more reliably using other methods.

How to push the array of data to another array through javascript without loop

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

Does jQuery have something like .map() which returns an object rather than an array?

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

Returning number of elements of JSON object

I need to iterate over every element in a JSON object, and I'm having trouble working out a way to count the number of elements in that object so I can use a for loop to iterate. Here's my object:
this.worldData =[
{"0":{"1":"0", "2":"0"},
"1":{"1":"0", "2":"0"},
"2":{"1":"0", "2":"0"}}
];
And what I'm trying:
alert(this.worldData.length);
Problem is, it always returns 1, no matter how many elements I put into the JSON object.
Do you have control over the JSON data? The length is returning 1 because there is only one element in the array. This is because of the way the JSON data is structured here. If you want something easier to iterate over, you would want something like this:
this.worldData = [
{"1":"0","2":"0"},
{"1":"0","2":"0"},
{"1":"0","2":"0"}
]
Note that objects (denoted with {}) don't have a length property, while arrays (denoted with []) do.
You're wrapping all of your objects inside a single object (the first and last curly braces). Try this:
this.worldData =[
{"0":{"1":"0", "2":"0"}},
{"1":{"1":"0", "2":"0"}},
{"2":{"1":"0", "2":"0"}}
];
jsFiddle example.

Categories