2017-01-03 6 views
3

にオブジェクトデータのリストを編集したのは、私は、このオブジェクトを持っているとしましょう:ソート変更/配列

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

、その後、私はhmmmからID 30で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" 
     } 
    ] 
    } 
} 

私の質問は、私が編集した後、私は/並べ替えを変更することができる方法であり、 オブジェクト?誰もがlodashまたはネイティブとのいずれかの例を知っていれば、それに私を参照してください、私もLodashライブラリを使用して

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

:私はこのような上部に最近編集されたオブジェクトが欲しいです。前もって感謝します。

+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#somesplice it、unshiftの商品を検索することができます。操作するための変更およびスプライスとアンシフトするためにはforEachを使用して

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

あなたの問題に対する可能な解決策、。

0

すべてのこの返信は私にいくつかのアイデアを与えました。ここであなたはロダッシュでそれをする方法です。今更新した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

感謝。