I have got an objects of arrays.
The 1,2,3,4,are the title index.
I want to extract each array of objects for each title separately
Here is the object with index and its corresponding values as array of objects
Object {
"1": Array [
Object {
"@SCHUM_HODSHI": "107250.13",
"@TEUR": "מרכיבי שכר לתשלום",
},
Object {
"@SCHUM_HODSHI": "50.00",
"@TEUR": "שכר יסוד",
},
Object {
"@SCHUM_HODSHI": "13652.67",
"@TEUR": "ש.רגילות",
},
],
"2": Array [
Object {
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה",
},
],
"3": Array [
Object {
"@SCHUM_HODSHI": "3200.00",
"@TEUR": "תוספת פינוק",
},
],
"4": Array [
Object {
"@SCHUM_HODSHI": "11250.00",
"@TEUR": "תוספת ג'רג'ראת",
},
],
}
For example:-- under first index i.e. 1 ,
i want to display below array of objects and so on like this.
Array [
Object {
"@SCHUM_HODSHI": "107250.13",
"@TEUR": "מרכיבי שכר לתשלום",
},
Object {
"@SCHUM_HODSHI": "50.00",
"@TEUR": "שכר יסוד",
},
Object {
"@SCHUM_HODSHI": "13652.67",
"@TEUR": "ש.רגילות",
},
],
kindly let me know what logic to be implemented on this?
Thanks.
CodePudding user response:
Do it like this:
//create empty array
let Array = []
//and this is your Object
const Obj = {
"1": [
{
"@SCHUM_HODSHI": "107250.13",
"@TEUR": "מרכיבי שכר לתשלום",
},
{
"@SCHUM_HODSHI": "50.00",
"@TEUR": "שכר יסוד",
},
{
"@SCHUM_HODSHI": "13652.67",
"@TEUR": "ש.רגילות",
},
],
"2": [
{
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה",
},
{
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה",
},
],
"3": [
{
"@SCHUM_HODSHI": "3200.00",
"@TEUR": "תוספת פינוק",
},
],
"4": [
{
"@SCHUM_HODSHI": "11250.00",
"@TEUR": "תוספת ג'רג'ראת",
},
],
}
//now just push your Object to your Array
Array.push(Obj)
console.log("Array: ", Array)
//Below is the way you can get specific 1,2,3,4 index objects with the map function
Array.map(obj => console.warn("obj: ", obj[1]));
Hope this works for you.
CodePudding user response:
This code will convert the object to Array.
const Obj = {
1: [
{
"@SCHUM_HODSHI": "107250.13",
"@TEUR": "מרכיבי שכר לתשלום",
},
{
"@SCHUM_HODSHI": "50.00",
"@TEUR": "שכר יסוד",
},
{
"@SCHUM_HODSHI": "13652.67",
"@TEUR": "ש.רגילות",
},
],
2: [
{
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה",
},
{
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה",
},
],
3: [
{
"@SCHUM_HODSHI": "3200.00",
"@TEUR": "תוספת פינוק",
},
],
4: [
{
"@SCHUM_HODSHI": "11250.00",
"@TEUR": "תוספת ג'רג'ראת",
},
],
};
const idx = Object.keys(Obj).map((key) => {
return parseInt(key) - 1;
});
// this will store the contents of the array you want
const newArray = Array(idx 1);
for (let index of idx) {
newArray[index] = Obj[index 1][0];
}
CodePudding user response:
const obj = {
"1": [
{
"@SCHUM_HODSHI": "107250.13",
"@TEUR": "מרכיבי שכר לתשלום"
},
{
"@SCHUM_HODSHI": "50.00",
"@TEUR": "שכר יסוד"
},
{
"@SCHUM_HODSHI": "13652.67",
"@TEUR": "ש.רגילות"
}
],
"2": [
{
"@SCHUM_HODSHI": "182.55",
"@TEUR": "תשלום קיטנה"
}
],
"3": [
{
"@SCHUM_HODSHI": "3200.00",
"@TEUR": "תוספת פינוק"
}
],
"4": [
{
"@SCHUM_HODSHI": "11250.00",
"@TEUR": "תוספת ג'רג'ראת"
}
]
};
/* store each array in new lists container */
const lists = Object.keys(obj).map((index) => {
return obj[index];
});
/* iterate on each list */
lists.map((list) => console.log(...list));
Lets do in a very simple and straight forward way. Extract all the lists, save that into another variable for later use and then iterate