算法重新索引对象的数组插入或拖'N'降顺序变化之后的 [英] Algorithm to re-index an array of objects after insertion or drag 'n' drop order change

查看:170
本文介绍了算法重新索引对象的数组插入或拖'N'降顺序变化之后的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有对象,如一个流行的民歌,这些含行的索引数组;)

Assume I have an indexed array of objects, such as these containing lines of a popular folk song ;)

var lyrics = [
  {line : 2, words : "He's a lumberjack and he's okay"},
  {line : 1, words : "I'm a lumberjack and I'm okay"},
  {line : 3, words : "He sleeps all night and he works all day"}
];

我比较会根据每个对象的指数显示,在视图中的对象。我希望能够在这个阵列上执行三项任务:

My comparator will display the objects in the view according to each object's index. I want to be able to perform the three tasks on this array:

任务1)重新编制上拖动'东经降

Task 1) Reindex on drag 'n drop

重新安排通过拖放对象的顺序。假设我已经知道如何实现拖放。任务的例子:拖动他是一个伐木工人,他没事从索引1之后,我是一个伐木工人,我没事。 他是一个伐木工人,他没事现在应该占据指数2和我是一个伐木工人,我没事应该占据指数1。由此产生的阵列应该是:

Re-arrange the order of the objects via drag and drop. Assume I already know how to implement drag and drop. Task example: Drag "He's a lumberjack and he's okay" from index "1" to after "I'm a lumberjack and I'm okay". "He's a lumberjack and he's okay" should now occupy index "2" and "I'm a lumberjack and I'm okay" should occupy index "1". The resulting array should be:

var lyrics = [
  {line : 1, words : "I'm a lumberjack and I'm okay"},
  {line : 2, words : "He's a lumberjack and he's okay"},
  {line : 3, words : "He sleeps all night and he works all day"}
];

任务2)插入重指数

Task 2) Re-index on insert

添加的对象的任何点阵列中,重建索引的阵列中的所有项目。任务例如:添加我睡了一晚上,我整天工作对象的数组中的第二项。由此产生的阵列应该是:

Add an object to any point in the array, reindexing all the items in the array. Task example: Add a "I sleep all night and I work all day" object as the second item in the array. The resulting array should be:

var lyrics = [
  {line : 1, words : "I'm a lumberjack and I'm okay"},
  {line : 2, words : "I sleep all night and I work all day"},
  {line : 3, words : "He's a lumberjack and he's okay"},
  {line : 4, words : "He sleeps all night and he works all day"}
];

任务3)删除重新指数

Task 3) Re-index on delete

从数组中删除一个对象和重新索引数组中的所有项目。因此,例如,如果与索引3的对象被删除,所得的阵列应该是:

Delete an object from an array and reindex all the items in the array. So for example, if object with index "3" were deleted, the resulting array should be:

var lyrics = [
  {line : 1, words : "I'm a lumberjack and I'm okay"},
  {line : 2, words : "I sleep all night and I work all day"},
  {line : 3, words : "He sleeps all night and he works all day"}
];

我没有一个CS程度的一种,所以我难倒什么算法会帮我处理这个问题。有人能指出我朝着正确的方向?

I don't have a CS degree so I'm kind of stumped on what algorithm would help me handle this. Can someone point me in the right direction?

我正在使用JavaScript,因此如果任何人知道任何东西,它上面的,我所有的耳朵。

I'm working with javascript, so if anyone knows anything that does the above, I'm all ears.

推荐答案

我将完全简化您的整个结构:

I would totally simplify your entire structure:

使用,而不是存储一个额外的键本地JavaScript数组,()使用JavaScript指标作为关键,这意味着JavaScript的(如果使用得当)将对其进行管理对你来说,并使用更少的内存。

Use a native javascript array, instead of storing an extra key (line) use the javascript index as the key, which means javascript (if used properly) will manage it for you, and use less memory.

所以,我们已经有了一个字符串数组:

So we've got an array of strings:

var f = [];
f.push('first');
f.push('third');
f.push('fourth');

// reindex on insert
// lets insert second in the natural place

f.splice(1,0,'second'); // ["first", "second", "third", "fourth"]

// reindex on delete
// lets delete 'third'

f.splice(2,1); // ["first", "second", "fourth"]

等。

这篇关于算法重新索引对象的数组插入或拖'N'降顺序变化之后的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆