我如何将JSON对象转换为用逗号分隔的角字符串?

问题描述 投票:-3回答:3

输入:“颜色”:{“ orange”:“多汁的颜色”,“ skyBlue”:“天空颜色”,“ black”:“深色”,“ white”:“和平的颜色”,},

想要输出:多汁的颜色,天空的颜色,深色,平静的颜色。

任何帮助都会有所帮助!谢谢:)

angular methods angular7
3个回答
0
投票
const colors = {
   orange: "juicy color",
   skyBlue: "sky color",
   black: "dark color",
   white: "peaceful color"
};

const ColorsString = Object.values(colors).join(", ");

0
投票

尝试一下

this.output = Object.keys(this.colors).map(val => this.colors[val]).join(', ');

-3
投票

尝试这样:

Working Demo

 colors = {
    orange: "juicy color",
    skyBlue: "sky color",
    black: "dark color",
    white: "peaceful color"
  };

数组输出

this.arrayOutput = Object.keys(this.colors).map(item => this.colors[item]);

字符串输出

  output: string = "";
  constructor() {
    Object.keys(this.colors).forEach(item => {
      this.output += this.colors[item];
       this.output += ","
    });
  }
© www.soinside.com 2019 - 2024. All rights reserved.