在javascript中显示数组中的前一个元素

Posted

技术标签:

【中文标题】在javascript中显示数组中的前一个元素【英文标题】:Display previous element in an array in javascript 【发布时间】:2021-12-01 22:23:22 【问题描述】:

我有一组对象。我在过滤所有“@KAV”:“1”对象后尝试下面的一个。

let finalArray=kavArray[index-1];

kavArray 中存在的所有 "@KAV":"1" 对象。

在满足 "@KAV":"1" 存在的条件之前如何显示这些项目?

谢谢。

         [
          
            "@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"
          ,
          
          
        ]

我怎样才能实现这些元素?

【问题讨论】:

【参考方案1】:

您可以使用Array.filter() 过滤下一项具有键/值@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)

【讨论】:

我想在对象之前显示每个 "@KAV":"1"。@Harun Yilmaz @Chinmayee 我不确定我是否理解所需的输出。你也可以提供吗?【参考方案2】:

您可以使用 javascriptfilter 方法来完成。

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

filter 的文档

【讨论】:

以上是关于在javascript中显示数组中的前一个元素的主要内容,如果未能解决你的问题,请参考以下文章

我如何在Javascript中拥有可变长度数组的前3个元素[重复]

如何使用 Angular JS 排序和选择 json 数组中的前 5 个元素

选择JavaScript数组中的最后一个元素[重复]

如何从 Javascript 中的数组中删除重复项?

如何在 JavaScript 中只显示前两个对象?

删除对象数组中的重复元素Javascript [关闭]