search

Home  >  Q&A  >  body text

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


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! !

黄舟黄舟2764 days ago908

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-06-12 09:33:21

    Object.keys(对象)

    Also, objects are unordered and may go wrong

    reply
    0
  • 習慣沉默

    習慣沉默2017-06-12 09:33:21

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

    reply
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-06-12 09:33:21

    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.

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-06-12 09:33:21

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

    reply
    0
  • 学习ing

    学习ing2017-06-12 09:33:21

    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.

    reply
    0
  • 三叔

    三叔2017-06-12 09:33:21

    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)

    reply
    0
  • Cancelreply