Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对数组中对象数据的更改/编辑列表进行排序_Javascript_Arrays_Sorting_Object_Lodash - Fatal编程技术网

Javascript 对数组中对象数据的更改/编辑列表进行排序

Javascript 对数组中对象数据的更改/编辑列表进行排序,javascript,arrays,sorting,object,lodash,Javascript,Arrays,Sorting,Object,Lodash,假设我有一个对象: { "data": { "result": [ { "id": 25, "lineNo": "222", "description": "hello" }, { "id": 30, "lineNo": "765", "description": "hmmm" }, { "id": 31,

假设我有一个对象:

{
  "data": {
    "result": [
      {
        "id": 25,
        "lineNo": "222",
        "description": "hello"
      },
      {
        "id": 30,
        "lineNo": "765",
        "description": "hmmm"
      },
      {
        "id": 31,
        "lineNo": "112",
        "description": "last"
      }
    ]
  }
}
然后我将id 30中的
description
hmmm
更改为:

  {
    "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或native的任何示例,请向我咨询。提前感谢。

您可以搜索带有通缉
id的物品,然后将其和物品添加到阵列中

函数updateArrayAndMove(数组、id、键、值){
array.some(函数(o,i,a){
如果(o.id==id){
o[键]=值;
a、 非移位(a.拼接(i,1)[0]);
返回true;
}
});
}
var对象={data:{result:[{id:25,行号:“222”,描述:“hello”},{id:30,行号:“765”,描述:“hmmm”},{id:31,行号:“112”,描述:“last”}};
updateArrayAndMove(object.data.result,30,'description','should be first');
console.log(对象)
.as控制台包装{最大高度:100%!重要;顶部:0;}
var对象=
{
“数据”:{
“结果”:[
{
“id”:25,
“行号”:“222”,
“说明”:“你好”
},
{
“id”:30,
“行号”:“765”,
“说明”:“应在第一位”
},
{
“id”:31,
“行号”:“112”,
“说明”:“最后”
}
]
}
}
函数更改元素(id、键、值)
{
var changedId=-1;
var arrrurned=object.data.result.forEach(函数(元素,索引){
if(元素['id']==id)
{
元素[键]=值;
changedId=index;//将更改后的索引推送到数组中
}
});
//元素发生了变化
//使用“拼接”更改顺序
var元素=object.data.result.splice(changedId,1);
object.data.result.unshift(元素[0])
console.log(对象)
}

变更要素(30,“说明”,“应为第一”)所有这些回答给了我一些想法。下面是如何使用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)
例如:

参考:


感谢所有的回复。

这很有趣。提示,
array.prototype。每一个
都会让您开始。@Mouser,您需要更多地跟踪更改的对象。如果您可以控制更改将执行这些更改的代码,或者可以在那里放置一个钩子,那么您只需要保留一个包含对象更改的队列,然后使用一个简单的算法来获取更改的项,然后按照该顺序保留数组中的所有项。但是,如果无法控制这些更改发生的时间,则可能需要使用Object.OvidiuDolha。@OvidiuDolha,观察者也是一个不错但很棘手的解决方案。为什么不直接将更新后的元素与第一个元素交换,如下所示:
[data.result[0],data.result[k]=[data.result[k],data.result[0]
?当然,如果OP使用lodash,那么
getIndexOfId
调用可以替换为
\uuu.findIndex(object.data.result,item=>item.id==30)