javascript - 如何拼接字典javascript

标签 javascript angularjs dictionary

我有一本字典:

{
    1: {
        "title": title,
        "name": name,
        "etc": etc
    },
    2: {
        "title": title,
        "name": name,
        "etc": etc
    },
    3: {
        "title": title,
        "name": name,
        "etc": etc
    },
    4: {
        "title": title,
        "name": name,
        "etc": etc
    },
    5: {
        "title": title,
        "name": name,
        "etc": etc
    },
    6: {
        "title": title,
        "name": name,
        "etc": etc
    }
}

我尝试了类似的方法,但工作没有改变..

我的问题是..我怎样才能将这本词典分成更多词典(页面)。 我想稍后将其用于 angularjs 分页。
任何想法或建议将不胜感激。谢谢!

最佳答案

你应该考虑使用数组来代替,因为 javascript 中的对象并不打算遵守这样的固定顺序,所以你使用像数组这样的字典很奇怪 - 无论如何:

function spliceDict(dict, minKey, maxKey) {
  var newDict = {};
  for(var i in dict) {
    if(i >= minKey && i <= maxKey) {
      newDict[i] = dict[i];
    }
  }
  return newDict;
}

如果您有一个 dict,并且想要获取 2 和 4 之间(并包括)之间的键,请像这样调用此方法(注意:这再次假设字典的所有键都是数字):

var splicedDict = spliceDict(dict, 2, 4);

关于javascript - 如何拼接字典javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33815682/

相关文章:

javascript - 你如何在 AngularJS View (ng-show) 中使用 promise ?

dictionary - 无法将 map 对象转换为 JSON 对象

python - 将 python 3.x 字典保存到 JSON

java - 将 HTML 内容保存在本地存储中

javascript - atom-typescript 更新 .ts 文件,但 html 中的更改不会在保存时更新

javascript - 添加新数组项时,使用外部模板重复的 angularjs 指令无法正确呈现。

javascript - Angular/grunt 无法加载模板

python - 在 python 字典中打印值时的间距

javascript - Ajax和FormData发送文件失败

javascript - javaScript 可以处理来自 ajax 调用的任何新添加的 DOM 元素吗