JSON-抓取完整对象并使用密钥抓取第二个

时间:2018-12-11 20:23:14

标签: javascript arrays json object

我希望我能在遇到问题时得到一些帮助。我不知道确切的用语,也许是对象中的嵌套对象,但我想知道如何获取一个JSON对象,然后在第一个对象中显示另一个对象。

我知道我需要运行一个循环并分配每个对象键。然后使用键抓住第二个对象以显示在第一个对象中。抱歉,我无法更好地解释它……但是我想要的输出将是这样的:

"id": 1,
"name": "Mark Twain",
"username": "MTwain",
"email": "MTwain@gmail.com",
"address": "1 Street Name"
 "child": {
      "id": 3,
       "name": "Matthew A James",
       "username": "MJames",
       "email": "MJames@gmail.com",
        "address": "3 Street Name" 
     }
 }

来自下面的JSON数组:

[
 {
"id": 1,
"name": "Mark Twain",
"username": "MTwain",
"email": "MTwain@gmail.com",
"address": "1 Street Name"

},

{
"id": 2,
"name": "Patricia Lebsack",
"username": "Karianne",
"email": "Julianne.OConner@kory.org",
"address": "2 Street Name"
},

{
"id": 3,
"name": "Matthew A James",
"username": "MJames",
"email": "MJames@gmail.com",
"address": "3 Street Name"
  },

{
"id": 4,
"name": "Gregory Switez",
"username": "GSwitez",
"email": "GSwitez@gmail.com",
"address": "4 Street Name"
}
]

编辑 我已经做了一个循环,可以执行我想要的操作,只是它可以显示对象4次(因为显然是循环)...想知道是否可以过滤它仅显示一次对象。

     for (var i = 0; i < array.length; i++) {
           for (var key in array[i]) {
              if(array[i].id == 1) {
              var newArray = array[i];
               newArray['objectTwo'] = array[2];
               console.log(newArray);
             }
          }
       }

1 个答案:

答案 0 :(得分:0)

假设objectOne已经存在,则可以从JSON数组中添加一个对象,如下所示:

objectOne['objectTwo'] = JSON_Array[2]

或者如果objectOne不存在,并且您想从数组中提取两个对象,则可以这样做。

let objectOne = JSON_Array[0]

objectOne['objectTwo'] = JSON_Array[2]

希望这会有所帮助! :)

编辑:这是在JSON_Array上使用map()来构建对象数组的示例,每个对象都包含一个子对象,该子对象是JSON_Array中当前索引的下一个对象。这可能不完全是您想要的,但希望可以在创建过程中为您提供帮助!

const JSON_Array = [
    {
        "id": 1,
        "name": "Mark Twain",
        "username": "MTwain",
        "email": "MTwain@gmail.com",
        "address": "1 Street Name"

    },

    {
        "id": 2,
        "name": "Patricia Lebsack",
        "username": "Karianne",
        "email": "Julianne.OConner@kory.org",
        "address": "2 Street Name"
    },

    {
        "id": 3,
        "name": "Matthew A James",
        "username": "MJames",
        "email": "MJames@gmail.com",
        "address": "3 Street Name"
    },

    {
        "id": 4,
        "name": "Gregory Switez",
        "username": "GSwitez",
        "email": "GSwitez@gmail.com",
        "address": "4 Street Name"
    }
]

const newArray = JSON_Array.map((item, index, array) => {
    if (array[index + 1]) {
        return {
            ...item,
            child: array[index + 1]
        }
    } else {
        return item
    }
})

console.log(newArray)

/* Expected output:

[ { id: 1,
    name: 'Mark Twain',
    username: 'MTwain',
    email: 'MTwain@gmail.com',
    address: '1 Street Name',
    child: 
     { id: 2,
       name: 'Patricia Lebsack',
       username: 'Karianne',
       email: 'Julianne.OConner@kory.org',
       address: '2 Street Name' } },
  { id: 2,
    name: 'Patricia Lebsack',
    username: 'Karianne',
    email: 'Julianne.OConner@kory.org',
    address: '2 Street Name',
    child: 
     { id: 3,
       name: 'Matthew A James',
       username: 'MJames',
       email: 'MJames@gmail.com',
       address: '3 Street Name' } },
  { id: 3,
    name: 'Matthew A James',
    username: 'MJames',
    email: 'MJames@gmail.com',
    address: '3 Street Name',
    child: 
     { id: 4,
       name: 'Gregory Switez',
       username: 'GSwitez',
       email: 'GSwitez@gmail.com',
       address: '4 Street Name' } },
  { id: 4,
    name: 'Gregory Switez',
    username: 'GSwitez',
    email: 'GSwitez@gmail.com',
    address: '4 Street Name' } ] */

EDIT2:这是一个使用for()循环的示例。

let newArray = []

const length = JSON_Array.length

for (let i = 0; i < length; i++) {
    if (i < (length - 1)) {
        newArray.push(
            {
                ...JSON_Array[i],
                child: JSON_Array[i+1]
            }
        )
    } else {
        newArray.push(JSON_Array[i])
    }
}

console.log(newArray)