如何从JSON字典中按键检索随机JSON对象? [英] How to retrieve random JSON object by key from JSON dictionary?

查看:92
本文介绍了如何从JSON字典中按键检索随机JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JSON对象,它由一长串其他JSON对象组成,这些对象有一些共同的属性,如:

I have a JSON object which consists of a long list of other JSON objects which have some common properties to each other such :

var myData = { 
    "0291" : { "Firstname" : "Jeremy", "Surname" : "Dyson" },
    "0398" : { "Firstnname" : "Billy", "Surname" : "Bunter" },
    "6714" : { "Firstnname" : "Harry", "Surname" : "Peterson" },
    "9080" : { "Firstnname" : "Barry", "secondname": "Joe", "Surname" : "Mainwaring"}
    ...
    ...
}

我已经构建了一个html模板。使用JS,我想通过随机顺序中的数据{}中的对象选择或迭代(随机选择+循环),这样我就可以为每个访问者动态填写HTML。随机部分很重要,因此每个访问者可能会得到不同的数据。

I already built an html template. With the JS, I want to pick or iterate (random pick + loop) through the objects in data{} in random order, so I can fill up the HTML on the fly for each visitor. The random part is important, so each visitor likely get a different data.

普通的JavaScript或jQuery解决方案将在部署它的上下文中工作。

Plain JavaScript or jQuery solutions will work in the context in which this is being deployed.

编辑:我实施的解决方案如下。

Solution I implemented is below.

<强> 1。收集所有密钥:

var keyArray = Object.keys(myData);

2。 随机播放功能

2. Shuffle function:

function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};
keyArray = shuffle(keyArray); // shuffle it!

3。循环迭代:

for (var i = 0; i < keyArray.length; ++i) {
    var current = data[keyArray[i]];
    ... // what you want to do each time.
}


推荐答案

首先,转换 data 数组的对象(第一级的键将丢失),如下所示: https://stackoverflow.com/a/11474071/664108

First, convert the data object to an array (the keys on the first level will be lost), like shown here: https://stackoverflow.com/a/11474071/664108

var dataArray = $.map(data, function (value, key) { return value; });

然后将阵列洗牌(参见如何随机化(混洗)一个JavaScript数组?

Then shuffle the array (see How to randomize (shuffle) a JavaScript array?)

或者你可以随便洗牌键然后操作原始对象。这样你还有钥匙:

Alternatively you could just shuffle the keys and then operate on the original object. This way you also have still the keys:

var keyArray = $.map(data, function (value, key) { return key; });

shuffle(keyArray); // example (shuffle function has to be implemented, see above)

for (var i = 0; i < keyArray.length; ++i) {
    var current = data[keyArray[i]];
    // do stuff with current dataset
}

来自评论

关键数组也可以通过以下方式创建:

the key array can also be created by:

var keyArray = Object.keys(data);

但请注意,这仅适用于现代浏览器,如果您想支持互联网,则不应使用它资源管理器版本高达IE 8(参见: http://msdn.microsoft.com/en-us/library/ie/ff688127(v = vs.94).aspx

But note that this only works on modern browsers, you should not use it if you want to support Internet Explorer in versions up to IE 8 (see: http://msdn.microsoft.com/en-us/library/ie/ff688127(v=vs.94).aspx)

这篇关于如何从JSON字典中按键检索随机JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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