【问题标题】:Find array of objects in next array to replace在下一个数组中查找要替换的对象数组
【发布时间】:2020-07-13 08:27:23
【问题描述】:

我有两个下面的数组对象。我想找到第一个数组的所有这些 ID,并使它们在第二个对象数组中处于活动状态,其余的必须在状态属性中变为无。

var searchIDs = [{ "id":"6001", "other" : "..." }, 
{ "id":"6002", "other" : "..."}] 

var list = [ 
    {"id":"9666", "status":"active"},
    {"id":"9667", "status":"none"},
    {"id":"9999", "status":"none"},
    {"id":"9668", "status":"none"},
    {"id":"9669", "status":"active"},
    {"id":"6001", "status":"none"},
    {"id":"6002", "status":"none"},
    {"id":"6003", "status":"none"},
    {"id":"6004", "status":"none"},
    {"id":"6005", "status":"active"},
    {"id":"6006", "status":"none"},
    {"id":"6007",  "status":"none"},
    {"id":"6008",  "status":"none"},
    {"id":"6009",  "status":"none"}
]



for (var i = 0 ; i < searchIDs.length ; i ++) {

    list.find(v => v.id !== searchIDs[i].id).status = "none";
    list.find(v => v.id === searchIDs[i].id).status = "active";
    Array.prototype.push.apply(list); 


}



console.log('Final List : '  + JSON.stringify(list)); 

但是当我尝试使用 for 循环的 find 函数时,它现在不起作用。我该如何解决?感谢阅读我的问题!谢谢!

最终结果应该是


[ 
    {"id":"9666", "status":"none"},
    {"id":"9667", "status":"none"},
    {"id":"9999", "status":"none"},
    {"id":"9668", "status":"none"},
    {"id":"9669", "status":"none"},
    {"id":"6001", "status":"active"},
    {"id":"6002", "status":"active"},
    {"id":"6003", "status":"none"},
    {"id":"6004", "status":"none"},
    {"id":"6005", "status":"none"},
    {"id":"6006", "status":"none"},
    {"id":"6007",  "status":"none"},
    {"id":"6008",  "status":"none"},
    {"id":"6009",  "status":"none"}
]
    

【问题讨论】:

    标签: javascript arrays arrayobject


    【解决方案1】:

    如果我正确理解了这个问题。这可能就是你要找的东西

    var searchIDs = [
        { "id":"6001", "other" : "..." }, 
        { "id":"6002", "other" : "..."}
    ] 
    
    var list = [ 
        {"id":"9666", "status":"active"},
        {"id":"9667", "status":"none"},
        {"id":"9999", "status":"none"},
        {"id":"9668", "status":"none"},
        {"id":"9669", "status":"active"},
        {"id":"6001", "status":"none"},
        {"id":"6002", "status":"none"},
        {"id":"6003", "status":"none"},
        {"id":"6004", "status":"none"},
        {"id":"6005", "status":"active"},
        {"id":"6006", "status":"none"},
        {"id":"6007",  "status":"none"},
        {"id":"6008",  "status":"none"},
        {"id":"6009",  "status":"none"}
    ]
    
    // This line creates a new array with only the id's in it
    const ids = searchIDs.map(line => line.id)
    
    
    // We will map this array
    // if the this line id appears in the ids list we can set the status to active. otherwise to none
    var newList = list.map(line => {
      // you could also use .includes
      line.status = ids.indexOf(line.id) > -1 ? 'active' : 'none';
      return line;
    })
    
    console.log(newList)

    【讨论】:

      【解决方案2】:

      这行得通。

        var searchIDs = [
         { "id":"6001", "other" : "..." }, 
         { "id":"6002", "other" : "..."}
        ] 
        var list = [ 
          {"id":"9666", "status":"active"},
          {"id":"9667", "status":"none"},
          {"id":"9999", "status":"none"},
          {"id":"9668", "status":"none"},
          {"id":"9669", "status":"active"},
          {"id":"6001", "status":"none"},
          {"id":"6002", "status":"none"},
          {"id":"6003", "status":"none"},
          {"id":"6004", "status":"none"},
          {"id":"6005", "status":"active"},
          {"id":"6006", "status":"none"},
          {"id":"6007",  "status":"none"},
          {"id":"6008",  "status":"none"},
          {"id":"6009",  "status":"none"}
        ]
      
        let resArr =  list.map(function(item){
           let obj = item;
           if(searchIDs.find(x => x.id == item.id)){
             obj.status = "active";
           }else{
             obj.status = "none";
           }
           return obj;
        });
        console.log(resArr);
      

      【讨论】:

        【解决方案3】:

        我建议在第二个列表 (list) 上的 Array.map 调用中搜索第一个列表 (searchIDs) 中的项目,我们实际上是在检查列表的每个成员是否也是 searchIDs 的成员。

        如果 searchID 非常大,这可能不是超级高效,因此可以创建一个 Set 并使用 Set.has 检查成员资格。

        var searchIDs = [{ "id":"6001", "other" : "..." }, 
        { "id":"6002", "other" : "..."}] 
        
        var list = [ 
            {"id":"9666", "status":"active"},
            {"id":"9667", "status":"none"},
            {"id":"9999", "status":"none"},
            {"id":"9668", "status":"none"},
            {"id":"9669", "status":"active"},
            {"id":"6001", "status":"none"},
            {"id":"6002", "status":"none"},
            {"id":"6003", "status":"none"},
            {"id":"6004", "status":"none"},
            {"id":"6005", "status":"active"},
            {"id":"6006", "status":"none"},
            {"id":"6007",  "status":"none"},
            {"id":"6008",  "status":"none"},
            {"id":"6009",  "status":"none"}
        ]
        
        
        list = list.map(r => { 
            return { id: r.id, status: searchIDs.find(s => s.id === r.id) ? "active": "none" };
        })
        
        console.log(list);

        【讨论】:

        • 是的,它适用于地图,列表可能还有其他属性,所以我必须选择 Kwarkjes 的答案。
        【解决方案4】:

        下面用一个非常基本的方法发帖,希望对你有所帮助。

        var searchIDs = [{ "id":"6001", "other" : "..." }, 
            { "id":"6002", "other" : "..."}] 
            
            var list = [ 
                {"id":"9666", "status":"active"},
                {"id":"9667", "status":"none"},
                {"id":"9999", "status":"none"},
                {"id":"9668", "status":"none"},
                {"id":"9669", "status":"active"},
                {"id":"6001", "status":"none"},
                {"id":"6002", "status":"none"},
                {"id":"6003", "status":"none"},
                {"id":"6004", "status":"none"},
                {"id":"6005", "status":"active"},
                {"id":"6006", "status":"none"},
                {"id":"6007",  "status":"none"},
                {"id":"6008",  "status":"none"},
                {"id":"6009",  "status":"none"}
            ]
            
            let newsearchIds = [];
            
            for (var i = 0 ; i < searchIDs.length ; i ++) {
                for(let key in searchIDs[i]){
                    newsearchIds.push(searchIDs[i][key]);
                }
            }
            
            for (var i = 0 ; i < list.length ; i ++) {
                for (var j = 0 ; j < newsearchIds.length ; j ++) {
                    if(list[i].id === newsearchIds[j]){
                        list[i].status= "active";
                    }
                }
            }
            console.log(list);
        

        【讨论】:

          猜你喜欢
          • 2014-08-20
          • 2022-10-25
          • 2018-10-27
          • 1970-01-01
          • 1970-01-01
          • 2019-07-19
          • 2021-04-22
          • 2017-12-26
          • 1970-01-01
          相关资源
          最近更新 更多