如何从数组中获取值?

时间:2016-01-17 09:33:17

标签: javascript jquery arrays

我有一个格式如下:

android:configChanges="orientation|screenSize"

现在我想将值RBS: [ { "RegExp": "", "Type": "" } ], PAN: [ { "RegExp": "Date", "Type": "Date" } ] 传递给方法,它应该获得PAN长度为1的计数并获得正则表达式值PAN和类型值。我怎样才能做到这一点?我形成了一个这样的数组:PAN包含NameRBS

PAN

2 个答案:

答案 0 :(得分:0)

var obj = {
  RBS:[{"RegExp":"","Type":""}],
  PAN:[{"RegExp":"Date","Type":"Date"}] 
};

function getTypeAndValue( obj, value )
{
  var output;
  var keys = Object.keys( obj );
  if ( obj [value ] )
  {
    output = obj[ value ][ 0 ];
  }
  return output;
}


var value = getTypeAndValue( obj, "PAN" );
if ( value )
{
  console.log( "type is " + value.Type + " and RegExp is  " + value.RegExp ); 
} 
else
{
   console.log( "this value doesn't exists" );
}

答案 1 :(得分:0)

你的意思是这样的吗?

<强> HTML

<div id="count"></div>
<div id="detail"></div>

<强> JAVASCRIPT

var countEl = document.getElementById("count");
var detailEl = document.getElementById("detail");


function Check(test){
  var count = test.length;
  countEl.innerHTML = "Count: " + count;
  detailEl.innerHTML = "";

  for (var i = 0 ; i < count ; i++){
    var values = test[i];
    detailEl.innerHTML += 
      "<br/>" + (i +1) + ":<br/>" +
      "RegExp: " + values["RegExp"] + "<br/>" +
      "Type: " + values["Type"] + "<br/>";

  }

}

var Name = {
   RBS: [  {
    "RegExp": "",
    "Type": ""
   } ],
   PAN: [ {
    "RegExp": "Date",
    "Type": "Date"
   } ]
};
Check(Name.PAN);

<强>样本

这是JSFiddle