javascript - I want to get the fifth attribute value of the object, but the sixth attribute name is not certain, so how to get the fifth attribute value
黄舟
黄舟 2017-06-12 09:31:21
0
6
853


These are the 7 attributes of the object, but the 6th 4.1.85 will change. In this way, how can we get the 6th attribute without relying on the attribute name? . . .
Using the object.key(object) method, the returned results are sorted, so it is still not fixed every time I retrieve it! !

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(6)
小葫芦
Object.keys(对象)

Also, objects are unordered and may go wrong

習慣沉默

The attribute name should be unchanged, right? What should be obtained is the corresponding value?

曾经蜡笔没有小新

Then you use the elimination method, loop through all the attributes of the current object, exclude the inherent attributes, and then get the unknown attribute, provided that your other attribute names are fixed.

phpcn_u1582

Exclusion method or regular matching (if there are fixed rules)

学习ing

The default key is fixed, so it can be traversed through $.each(), as follows:

var object = {
    name : "张三",
    age : 22,
    tell : 1234565678,
    height : 180,
    num : Math.floor(Math.random()*10+1),
    color : "red"
}

$.each(object,function(key,val){
    if(val === object.num ){
            console.log(val);
        }
    })

Even if the object is out of order, it can be traversed through $.each(), and then judged by conditions. Hope it will be adopted.

三叔

Use Object.entries(obj)

var car = {type:"Fiat", model:"500", color:"white"};
console.log(Object.entries(car));

The output is:

type,Fiat,model,500,color,white

The matching order can be guaranteed.

In addition, the properties of Object are not guaranteed to be ordered (different from guaranteed to be unordered)

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template