在java脚本中使用Slice on string

时间:2018-01-04 09:43:52

标签: javascript angularjs oop split slice

嗨,我喜欢这样的字符串:

[{"list":"Gross Motor Skills""value":"8"}{""list":"Sorting""value":"8"}
 {""list":"Grouping""value":"8"}{""list":"Drawing""value":"8"}
 {""list":"Observations""value":"6"}{""list":"Writing""value":"7"}]"

我希望删除所有额外的东西并将其放入数组中:

Gross Motor Skills 8
Sorting 8 
etc. 

我尝试过拼接和分裂,但我没有从中得到任何东西。任何人都可以帮我搞定吗?提前致谢。

2 个答案:

答案 0 :(得分:0)

使用正确的数组/对象,您可以使用Object.values作为值。

var array = [{ list: "Gross Motor Skills", value: "8" }, { list: "Sorting", value: "8" }, { list: "Grouping", value: "8" }, { list: "Drawing", value: "8" }, { list: "Observations", value: "6" }, { list: "Writing", value: "7" }],
    result = array.map(o => Object.values(o));

console.log(result);

答案 1 :(得分:0)

  

我希望删除所有额外的东西并将其放入数组中:

Gross Motor Skills 8
Sorting 8 
etc.

使用mapjoin

var output = arr.map( s => s.list.concat( s.value ) ).join( "\n" );

<强>演示

var arr = [{
    "list": "Gross Motor Skills",
    "value": "8"
  }, {
    "list": "Sorting",
    "value": "8"
  },
  {
    "list": "Grouping",
    "value": "8"
  }, {
    "list": "Drawing",
    "value": "8"
  }, {
    "list": "Observations",
    "value": "6"
  }, {
    "list": "Writing",
    "value": "7"
  }
];
var output = arr.map( s => s.list + " - " + s.value ).join( "\n" );
console.log( output );

或者如果需要将数组作为输出,则省略join

var output = arr.map( s => s.list + " - " + s.value );