2017-01-03 27 views
3

比方說,我有這樣的對象:排序更改/數組對象數據的編輯列表

{ 
    "data": { 
    "result": [ 
     { 
     "id": 25, 
     "lineNo": "222", 
     "description": "hello" 
     }, 
     { 
     "id": 30, 
     "lineNo": "765", 
     "description": "hmmm" 
     }, 
     { 
     "id": 31, 
     "lineNo": "112", 
     "description": "last" 
     } 
    ] 
    } 
} 

,然後我在ID 30從hmmm改變description到:

{ 
    "id": 30, 
    "lineNo": "765", 
    "description": "should be first" 
    }, 

,將成爲像這樣:

{ 
    "data": { 
    "result": [ 
     { 
     "id": 25, 
     "lineNo": "222", 
     "description": "hello" 
     }, 
     { 
     "id": 30, 
     "lineNo": "765", 
     "description": "should be first" 
     }, 
     { 
     "id": 31, 
     "lineNo": "112", 
     "description": "last" 
     } 
    ] 
    } 
} 

我的問題是,如何在編輯後更改/排序 物體?我想在這樣的頂部最近編輯的對象:

{ 
    "data": { 
    "result": [ 
     { 
     "id": 30, 
     "lineNo": "765", 
     "description": "should be first" 
     }, 
     { 
     "id": 25, 
     "lineNo": "222", 
     "description": "hello" 
     }, 
     { 
     "id": 31, 
     "lineNo": "112", 
     "description": "last" 
     } 
    ] 
    } 
} 

我也使用Lodash庫,如果有人知道與lodash或原生請參考我給它的任何實例。提前致謝。

+0

這很有趣。一個提示,'array.prototype.every'會讓你開始。 – Mouser

+1

@Mouser,more ['forEach'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) –

+0

您需要跟蹤更改的對象。如果您有控制權來更改將執行這些更改的代碼,或者可以在其中放置一個鉤子,那麼您只需要保留一個隊列中的對象更改,然後使用一種簡單的算法來更改項目,然後將數組中剩餘的所有項目該訂單。但是,如果您無法控制何時發生這些更改,則可能需要使用Object.observe。 –

回答

2

你可以搜索與希望id項目與Array#somespliceunshift項目的數組。

function updateArrayAndMove(array, id, key, value) { 
 
    array.some(function (o, i, a) { 
 
     if (o.id === id) { 
 
      o[key] = value; 
 
      a.unshift(a.splice(i, 1)[0]); 
 
      return true; 
 
     } 
 
    }); 
 
} 
 

 
var object = { data: { result: [{ id: 25, lineNo: "222", description: "hello" }, { id: 30, lineNo: "765", description: "hmmm" }, { id: 31, lineNo: "112", description: "last" }] } }; 
 

 
updateArrayAndMove(object.data.result, 30, 'description', 'should be first'); 
 
console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+1

如果OP使用lodash,那麼getIndexOfId調用可以用_.findIndex(object.data.result,item => item.id === 30 )'。 – Assan

1

var object = 
 
{ 
 
    "data": { 
 
    "result": [ 
 
     { 
 
     "id": 25, 
 
     "lineNo": "222", 
 
     "description": "hello" 
 
     }, 
 
     { 
 
     "id": 30, 
 
     "lineNo": "765", 
 
     "description": "should be first" 
 
     }, 
 
     { 
 
     "id": 31, 
 
     "lineNo": "112", 
 
     "description": "last" 
 
     } 
 
    ] 
 
    } 
 
} 
 

 
function changeElement(id, key, value) 
 
{ 
 
    var changedId = -1; 
 
    var arrReturned = object.data.result.forEach(function(element, index){ 
 
    if (element['id'] == id) 
 
    { 
 
     element[key] = value; 
 
     changedId = index; //push the changed index into the array 
 
    } 
 
    }); 
 
    
 
    //element has changed 
 
    //use splice to change the order 
 
    var element = object.data.result.splice(changedId, 1); 
 
    object.data.result.unshift(element[0]) 
 
    
 
    console.log(object) 
 
} 
 

 
changeElement(30, "description", "should be first");

一個可能的解決您的問題,及如何使用foreach改變和接頭和不印字操縱。

0

所有此回覆給了我一些想法。以下是你如何使用lodash做到的。你必須知道你剛剛更新的id對象。

var object = 
    { 
     "data": { 
     "result": [ 
      { 
      "id": 25, 
      "lineNo": "222", 
      "description": "hello" 
      }, 
      { 
      "id": 30, 
      "lineNo": "765", 
      "description": "should be first" 
      }, 
      { 
      "id": 31, 
      "lineNo": "112", 
      "description": "last" 
      } 
     ] 
     } 
    } 

    var filteredObj = _.remove(object.data.result, function(o) { 
     return o.id == '30'; // must know object id 
    }); 
    object.data.result.unshift(filteredObj[0]); 

    console.log("mutated object.data.result", object.data.result) 

實施例:https://fiddle.jshell.net/uLqfuqn1/

參考:https://lodash.com/docs/4.17.4#remove

由於所有的回覆。