如果不存在则如何更新数组中的json添加

时间:2019-11-22 10:44:34

标签: javascript json

如果存在于数组中,我需要更新json(whitelistype和时间戳)。如果不存在,则仅添加。我提供了json的示例。白名单是:

 {Url: "apple.com", WhiteListType: 1, timestamp: 921421321321}
 {Url: "google.com", WhiteListType: 3, timestamp: 721421321321}
 {Url: "telegraf.rs", WhiteListType: 3, timestamp: 521421321321}

我不得不辩护。如果添加和删除。如果添加,我将提供代码:

    for (var i = 0; i < Whitelist.length; i++) { 
        if ( Whitelist[i].WhiteListType === 3 && Whitelist[i].Url == Preventer[data.WindowId].CurrentDomain ) {
            console.log("free")
            // Preventer[data.WindowId].CurrentDomain this is only domain on current site which is in tab
            // Preventer[data.WindowId].CurrentDomain is our Url in json
            // example of Preventer[data.WindowId].CurrentDomain is "apple.com"
            let req3 = {
                Url: Preventer[data.WindowId].CurrentDomain,
                WhiteListType: 0,
                timestamp: Date.now()
            }
                Whitelist.unshift(req3);
                console.log("Added white list" , Whitelist) 
                // example Whitelist json is {Url: "apple.com", WhiteListType: 1, timestamp: 921421321321}
                Whitelist2.unshift(Preventer[data.WindowId].CurrentDomain)
            return  webSocketClient.SendMessage("newWhiteList%" + JSON.stringify(Whitelist)); 

        }else if (Whitelist[i].WhiteListType === 1 && Whitelist[i].Url === Preventer[data.WindowId].CurrentDomain) {
            console.log("Jedan")
            let req3 = {
                Url: Preventer[data.WindowId].CurrentDomain,
                WhiteListType: 2,
                timestamp: Date.now()
            }
                Whitelist.unshift(req3); 
                console.log("Added white list" , Whitelist) 
                Whitelist2.unshift(Preventer[data.WindowId].CurrentDomain)
                return  webSocketClient.SendMessage("newWhiteList%" + JSON.stringify(Whitelist)); 
        } else   {
            console.log("two situation")
            let req5 = {
                Url: Preventer[data.WindowId].CurrentDomain,
                WhiteListType: 2,
                timestamp: Date.now()
            }
                Whitelist.unshift(req5); 
                console.log("Added white list" , Whitelist) 
                Whitelist2.unshift(Preventer[data.WindowId].CurrentDomain)
                console.log("@" ,Whitelist2 )
                return  webSocketClient.SendMessage("newWhiteList%" + JSON.stringify(Whitelist)); 
        } 
    }
} 

我的问题。

>如果json中的Url仅更改-更新WhiteListType和时间戳,而不添加新的json,怎么办??

1 个答案:

答案 0 :(得分:0)

  • 使用forEach遍历原始数组的元素。

  • 如果元素与所需的Url和WhiteListType相匹配,则进行更新 并标记该元素已经存在。

  • 浏览完所有元素后,如果标志表明它 不存在,请将其添加到数组中。

这里是一个例子:

let originalArray = [{
    Url: "apple.com",
    WhiteListType: 1,
    timestamp: 921421321321
  },
  {
    Url: "google.com",
    WhiteListType: 3,
    timestamp: 721421321321
  },
  {
    Url: "telegraf.rs",
    WhiteListType: 3,
    timestamp: 521421321321
  }
]

const newObject1 = {
  Url: "google.com",
  WhiteListType: 3,
  timestamp: 999999
}

const newObject2 = {
  Url: "yahoo.com",
  WhiteListType: 2,
  timestamp: 999999
}

const resultNode = document.getElementById("result")

function action(newObject) {
  let objectAlreadyExisted = false

  let result = originalArray.map(element => {
    let newElement = { ...element }

    if (newElement.Url === newObject.Url && newElement.WhiteListType === newObject.WhiteListType) {
      objectAlreadyExisted = true
      newElement.timestamp = newObject.timestamp
    }
    return newElement
  })

  if (!objectAlreadyExisted) {
    result.push(newObject)
  }

  resultNode.innerHTML = JSON.stringify(result)

}

function reset() {
  resultNode.innerHTML = JSON.stringify(originalArray)
}

document.onload = reset()
<button onclick="action(newObject1)">action for newObject1</button>
<button onclick="action(newObject2)">action for newObject2</button>
<button onclick="reset()">reset</button>
<div id="result"></div>