Get element of JS object with an index

后端 未结 7 1166
南方客
南方客 2021-01-30 20:43

Ok so let\'s say that I have my object

myobj = {\"A\":[\"Abe\"], \"B\":[\"Bob\"]}

and I want to get the first element out of it. As in I want i

7条回答
  •  南方客
    南方客 (楼主)
    2021-01-30 21:29

    var myobj = {"A":["Abe"], "B":["Bob"]};
    
    var keysArray = Object.keys(myobj);
    
    var valuesArray = Object.keys(myobj).map(function(k) {
    
       return String(myobj[k]);
    
    });
    
    var mydata = valuesArray[keysArray.indexOf("A")]; // Abe
    

提交回复
热议问题