Javascript Get flat array from object contain multiple arrays - javascript

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

Related

Why is the type of array created using array function a object?

When a array is created using the array function as shown below.
Let array = Array(10);
And console logging the typeof array it shows object but why ?
I saw this.
Arrays are also type of objects.
If you want to identify if its array or not you can use Array.isArray()
if you check the data types array is not defined

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.

javascript push associative array in another

I have two associative arrays and want to push one completely to the other.
my current code:
LT_WORK_PACKAGE.data[i].STOCK_DOCCAT = stock_data.STOCK_DOCCAT;
LT_WORK_PACKAGE.data[i].STOCK_DOCNO = stock_data.STOCK_DOCNO;
LT_WORK_PACKAGE.data[i].STOCK_ITMNO = stock_data.STOCK_ITMNO;
im looking for something like this:
LT_WORK_PACKAGE.data[i].push(stock_data);
.push is for adding items to an array. You do have an object , and to copy multiple properties into an object,you can use Object.assign:
Object.assign(
/*to:*/ LT_WORK_PACKAGE.data[i],
/*from:*/ stock_data
);
You can use LT_WORK_PACKAGE.data[i] = stock_data.
Note that the previous content (if it exists) of LT_WORK_PACKAGE.data[i] will be replaced by a reference to stock_data. Any changes made in stock_data will be done in LT_WORK_PACKAGE.data[i] If you want a copy, you can use : LT_WORK_PACKAGE.data[i] = JSON.parse(JSON.serialize(stock_data))

Using index to pull items from an object

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.

cant create array with STRING key

Im trying to create an associative array with objects, the key should always be a string (but they are always numbers).
This is how i store them (recording user clicks):
App.Recording[currentTime.toString()] = {sound: buttonName.toLowerCase() };
When trying to do this:
var save = {};
save.recording = App.Recording;
console.log(JSON.stringify(save));
I get this:
{"recording":[null, null,{"sound":"e"},null,null,null,.......,null,null,null,null,{"sound":"e"},....,null, null...]}
So, the toString() doesnt work on currentTime.toString(), which make my array store currentTime as numbers instead...
How can I save the objects and have an associative array?
Have a look HERE first. There are no associative arrays in JS. Instead of an array, you should use an object with a for in loop.

Categories