动态json数组与一个数组的键相同

时间:2017-12-19 06:01:28

标签: javascript json node.js

我有一个json数组,如下所示,此数据来自Excel工作表并将其转换为json格式。

[{
  'booktitle': 'Leading',
  'bookid': '56353',
  'bookauthor': 'Sir Alex Ferguson'
}, {
  'booktitle': 'How Google Works',
  'bookid': '73638',
  'bookauthor': 'Eric Smith'
}, {
  'booktitle': 'The Merchant of Venice',
  'bookid': '37364',
  'bookauthor': 'William Shakespeare'
}]

我想在一个数组中使用相同的键获取值。这个键值对将是动态的,因此需要一般的解决方案。

3 个答案:

答案 0 :(得分:1)

我认为你在寻找的是这个。如果您想添加其他内容,请告诉我们。 检查代码段。



public void btnregisteradm(View v) {
        if (awesomeValidation.validate()) {
            admname = name.getText().toString();
            admpass = password.getText().toString();
            admemail = email.getText().toString();
            admmob = mob.getText().toString();
            //Toast.makeText(ctx, "we are here", Toast.LENGTH_SHORT).show();
           String method = "register";
            BackgroundTask backgroundTask = new BackgroundTask(this);
            backgroundTask.execute(method,admname,admemail,admpass,admmob);
            finish();

        }
    }




答案 1 :(得分:0)

let bookInfo = [{
  'booktitle': 'Leading',
  'bookid': '56353',
  'bookauthor': 'Sir Alex Ferguson'
}, {
  'booktitle': 'How Google Works',
  'bookid': '73638',
  'bookauthor': 'Eric Smith'
}, {
 'booktitle': 'The Merchant of Venice',
 'bookid': '37364',
 'bookauthor': 'William Shakespeare'
}]

  var bookTitle = [];
  var bookId   = [];
  var bookAuthor = [];

 bookInfo.filter(function(data){
      if(data.booktitle){
          bookTitle.push(data.booktitle);
       }

       if(data.bookid){
          bookId.push(data.bookid);
       }


       if(data.bookauthor){
          bookAuthor.push(data.bookauthor);
       }

 })

答案 2 :(得分:0)

您可以使用像这样的数组映射对象键

来完成此操作



let arr = [{
    "booktitle": "Leading",
    "bookid": "56353",
    "bookauthor": "Sir Alex Ferguson"
}, {
    "booktitle": "How Google Works",
    "bookid": "73638",
    "bookauthor": "Eric Smith"
}, {
    "booktitle": "The Merchant of Venice",
    "bookid": "37364",
    "bookauthor": "William Shakespeare"
}];

let ans=Object.keys(arr[0]).map((key) => {
    let o={};
    o[key]=arr.map((x) => x[key]);
    return o;
});

console.log(ans);