Sertai rentetan JSON
P粉035600555
P粉035600555 2024-04-01 14:36:21
0
2
454

Adakah terdapat cara khas untuk menggabungkan setiap blok rentetan individu dengan | seperti ini

9000234|测试 NPC|0|0|0|0|0|0|0|0| 不包括 isEnabled. Saya juga tidak mahu blok lain bergabung antara satu sama lain. Saya tidak pasti apa yang perlu dilakukan.

"100_npc": {
        "uniqueId": 9000234,
        "username": "Test NPC",
        "itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},
        "isEnabled": true
    },
    "101_npc": {
        "uniqueId": 9000251,
        "username": "Pelican",
        "itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},
        "isEnabled": true
    }
}

P粉035600555
P粉035600555

membalas semua(2)
P粉194919082

Saya hanya mahu mencari nama atribut tertentu, seperti "npc_100"

const data =  {  
  "100_npc": {"uniqueId": 9000234,"username": "Test NPC","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true},
  "101_npc": {"uniqueId": 9000251,"username": "Pelican","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true}
};


const prop = "100_npc";

const result = Object.entries(data[prop]).reduce((a, [k, v]) => 
  (k !== "isEnabled" && a.push(k==="itemsObj" ? Object.values(v) : v), a)
, []).flat().join("|");

console.log(result)

Untuk failOutput:

const data =  {  
  "100_npc": {"uniqueId": 9000234,"username": "Test NPC","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true},
  "101_npc": {"uniqueId": 9000251,"username": "Pelican","itemsObj": {"colour": 0, "head": 0, "face": 0, "body": 0, "neck": 0, "hand": 0, "feet": 0, "flag": 0, "photo": 0},"isEnabled": true}
};

const result = Object.values(data).map(entry => Object.entries(entry).reduce((a, [k, v]) => 
  (k !== "isEnabled" && a.push(k==="itemsObj" ? Object.values(v) : v), a)
, []).flat().join("|")).join("\n");

console.log(result)
P粉579008412

Ini nampaknya mengeluarkan apa yang anda minta: (taman permainan)

    let data = {
      '100_npc': {
        uniqueId: 9000234,
        username: 'Test NPC',
        itemsObj: {
          colour: 0,
          head: 0,
          face: 0,
          body: 0,
          neck: 0,
          hand: 0,
          feet: 0,
          flag: 0,
          photo: 0,
        },
        isEnabled: true,
      },
      '101_npc': {
        uniqueId: 9000251,
        username: 'Pelican',
        itemsObj: {
          colour: 0,
          head: 0,
          face: 0,
          body: 0,
          neck: 0,
          hand: 0,
          feet: 0,
          flag: 0,
          photo: 0,
        },
        isEnabled: true,
      },
    };
    
    let result = [];
    
    for (let key in data) {
      if (data.hasOwnProperty(key)) {
        let item = data[key];
        let arr = [item.uniqueId, item.username];
        for (let objKey in item.itemsObj) {
          arr.push(item.itemsObj[objKey]);
        }
        result.push(arr.join('|'));
      }
    }
    
    console.log(result.join('\n'));
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!