排序更改/数组对象数据的编辑列表
问题描述:
比方说,我有这样的对象:排序更改/数组对象数据的编辑列表
{
"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或原生请参考我给它的任何实例。提前致谢。
答
你可以搜索与希望id
项目与Array#some
,splice
并unshift
项目的数组。
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
答
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改变和接头和不印字操纵。
答
所有此回复给了我一些想法。以下是你如何使用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
由于所有的回复。
这很有趣。一个提示,'array.prototype.every'会让你开始。 – Mouser
@Mouser,more ['forEach'](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) –
您需要跟踪更改的对象。如果您有控制权来更改将执行这些更改的代码,或者可以在其中放置一个钩子,那么您只需要保留一个队列中的对象更改,然后使用一种简单的算法来更改项目,然后将数组中剩余的所有项目该订单。但是,如果您无法控制何时发生这些更改,则可能需要使用Object.observe。 –