简体   繁体   中英

How to split JSON data in Angular 2 and ionic 2

I need to separate the json file data, I create a function to separate the character string but it returns single strings:

"AKERMI ## 1988", "AKIM DE DUCOR ## 2005", ...

But I want to split this string into fields:

Name: AKERMI

Date: 1988

Name: AKIM DE DUCOR

Date: 2005

test.ts

  getDataEtalon() {
        return this.data.getEtalon().subscribe(
            data => {
                for (let key in data) {
                    this.etalon = data[key].split('##');
                }
                console.log(this.etalon);
            },
            err => {
                console.log(err);
            }
        );
    }

JSON

 {
    0: "AKERMI##1988",
    5: "AKIM DE DUCOR##2005",
    6: "AL FATIH##2003",
    9: "AMER##1984",
    13: "BELAMER##2004",
    15: "CHAHATA##1990",
    20: "CHARH##1999",
    23: "CHEIKH EL ARAB##1990",
    24: "DAHESS##1999",
    35: "DARIKE##1991",
    44: "DARMAN##1998",
    50: "DAYJUR##1991",
    51: "DJARNI DES FORGES##1995",
    55: "DJELMANE##1999",
    77: "DJOURDAN##1999",
    83: "DORMANE##1984",
    103: "FARADJALA##1993",
    105: "GITPEN##1994",
    110: "HAJJAM##1995",
    175: "HAKIM DU BAC##1995",
    180: "HALIM##1995",
    185: "HAMID##1996",
    194: "HAMZA##1995",
    204: "HYRAM##1995",
    206: "ISSAOUI##1996",
    210: "JAMAK##1997",
    213: "JEDEL##1997",
    231: "JESROY DE CHAILLAC##1997",
    232: "KAHLOUN##1998",
    233: "KERBELLA##1992",
    236: "LATHLETH##1999",
    246: "MAJD AL ARAB##2002",
    251: "MAKZAN##1991",
    258: "MANGANELLO##1990",
    259: "MANGUIER DE PIBOUL##1995",
    263: "MAYSOUN##2000",
    266: "MIGYESS##2000",
    269: "MOUSSOUL##2000",
    324: "MUNEEF##2001",
    334: "NADIM##2001",
    336: "NAKKACH##2001",
    337: "NEDJAM LOTOIS##1996",
    338: "NEMROD DU PAON##2001",
    345: "NEZ D'OR##1997",
    346: "NIZAM##1998",
    348: "ORIENT EXPRESS##1995",
    349: "OUAAD##2003",
    350: "PHARAON DES CEDRES##2000",
    351: "RAAD##2003",
    357: "RAEEH##2003",
    358: "RAFII##2003",
    366: "ROUSHAAN##2003",
    371: "SAMIR##1985",
    391: "SARKI D'ESPIENS##1992",
    392: "SAYAF##2004",
    398: "SEMAOU'EL##2004",
    400: "SOUR##1985",
    404: "SULTAN AL BADR##2004",
    405: "TIDJAM LOTOIS##1999",
    433: "TOUWAYSSAN##1986",
    434: "VENT DREDY##1992",
    435: "VIOLET##2001",
    436: "ZEIDOUN##1987"
    }
data.map(car => { 
let [name, year] = car.split('##');
return { name, year };
});

Will result in an array of objects with a name and a year

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM