I have got one array of objects.I am trying below one after filtering all "@KAV":"1" objects.
let finalArray=kavArray[index-1];
All the "@KAV":"1" object present in the kavArray.
How can i display those items before satisfying the condition where "@KAV":"1" is present?
Thanks.
[
{
"@USEFORMAT": "1",
"@TEUR": "מרכיבי שכר לתשלום",
"@SUG_TASHLUM": "6",
"@TAARIF": "104135.00",
"@SCHUM_HODSHI": "107250.13",
"@KAV": "2"
},
{
"@USEFORMAT": "1",
"@TEUR": "שכר יסוד",
"@SUG_TASHLUM": "6",
"@KAVUA": "9",
"@SCHUM_HODSHI": "50.00",
"@SEMEL": "1",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "ש.רגילות",
"@SUG_TASHLUM": "6",
"@SCHUM_HODSHI": "13652.67",
"@SEMEL": "101",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "שכר בסיס",
"@SCHUM_HODSHI": "13702.67",
"@KAV": "1"
},
{
"@USEFORMAT": "1",
"@TEUR": "תשלום קיטנה",
"@SUG_TASHLUM": "6",
"@TAARIF": "182.55",
"@KAMUT": "1.00",
"@KAVUA": "מק",
"@SCHUM_HODSHI": "182.55",
"@SEMEL": "90",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "החזר הוצאות",
"@SCHUM_HODSHI": "182.55",
"@KAV": "1"
},
{
"@USEFORMAT": "1",
"@TEUR": "תוספת פינוק",
"@SUG_TASHLUM": "6",
"@KAVUA": "או",
"@SCHUM_HODSHI": "3200.00",
"@SEMEL": "73",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "מענקים",
"@SCHUM_HODSHI": "3200.00",
"@KAV": "1"
},
]
How can i achieve those elements?
CodePudding user response:
You can use Array.filter()
to filter the elements whose next item has key/value @KAV:"1"
const kavArray = [ { "@USEFORMAT": "1", "@TEUR": "מרכיבי שכר לתשלום", "@SUG_TASHLUM": "6", "@TAARIF": "104135.00", "@SCHUM_HODSHI": "107250.13", "@KAV": "2" }, { "@USEFORMAT": "1", "@TEUR": "שכר יסוד", "@SUG_TASHLUM": "6", "@KAVUA": "9", "@SCHUM_HODSHI": "50.00", "@SEMEL": "1", "@KAV": "0" }, { "@USEFORMAT": "1", "@TEUR": "ש.רגילות", "@SUG_TASHLUM": "6", "@SCHUM_HODSHI": "13652.67", "@SEMEL": "101", "@KAV": "0" }, { "@USEFORMAT": "1", "@TEUR": "שכר בסיס", "@SCHUM_HODSHI": "13702.67", "@KAV": "1" }, { "@USEFORMAT": "1", "@TEUR": "תשלום קיטנה", "@SUG_TASHLUM": "6", "@TAARIF": "182.55", "@KAMUT": "1.00", "@KAVUA": "מק", "@SCHUM_HODSHI": "182.55", "@SEMEL": "90", "@KAV": "0" }, { "@USEFORMAT": "1", "@TEUR": "החזר הוצאות", "@SCHUM_HODSHI": "182.55", "@KAV": "1" }, { "@USEFORMAT": "1", "@TEUR": "תוספת פינוק", "@SUG_TASHLUM": "6", "@KAVUA": "או", "@SCHUM_HODSHI": "3200.00", "@SEMEL": "73", "@KAV": "0" }, { "@USEFORMAT": "1", "@TEUR": "מענקים", "@SCHUM_HODSHI": "3200.00", "@KAV": "1" }, ]
const filteredArray = kavArray.filter((item, index) => kavArray[index 1] && kavArray[index 1]["@KAV"] === "1")
console.log(filteredArray)
CodePudding user response:
you can use JavaScript's filter
method to accomplish that.
const arr = [
{
"@USEFORMAT": "1",
"@TEUR": "מרכיבי שכר לתשלום",
"@SUG_TASHLUM": "6",
"@TAARIF": "104135.00",
"@SCHUM_HODSHI": "107250.13",
"@KAV": "2"
},
{
"@USEFORMAT": "1",
"@TEUR": "שכר יסוד",
"@SUG_TASHLUM": "6",
"@KAVUA": "9",
"@SCHUM_HODSHI": "50.00",
"@SEMEL": "1",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "ש.רגילות",
"@SUG_TASHLUM": "6",
"@SCHUM_HODSHI": "13652.67",
"@SEMEL": "101",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "שכר בסיס",
"@SCHUM_HODSHI": "13702.67",
"@KAV": "1"
},
{
"@USEFORMAT": "1",
"@TEUR": "תשלום קיטנה",
"@SUG_TASHLUM": "6",
"@TAARIF": "182.55",
"@KAMUT": "1.00",
"@KAVUA": "מק",
"@SCHUM_HODSHI": "182.55",
"@SEMEL": "90",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "החזר הוצאות",
"@SCHUM_HODSHI": "182.55",
"@KAV": "1"
},
{
"@USEFORMAT": "1",
"@TEUR": "תוספת פינוק",
"@SUG_TASHLUM": "6",
"@KAVUA": "או",
"@SCHUM_HODSHI": "3200.00",
"@SEMEL": "73",
"@KAV": "0"
},
{
"@USEFORMAT": "1",
"@TEUR": "מענקים",
"@SCHUM_HODSHI": "3200.00",
"@KAV": "1"
},
];
const result = arr.filter(item => item["@KAV"] === "1");
console.log(result);
documentation for filter