简体   繁体   English

将对象数组转换为数组,但格式不同。

[英]Convert array of objects into an array but in a different format.

I have an array of objects as shown below. 我有一个对象数组,如下所示。 I did a console.log(values); 我做了一个console.log(values); and got below result 并低于结果

["15- S&P", "us- ex US", "al- ex CL"]

0:"15- S&P"
1:"us- ex US"
2:"al- ex CL"
length:3
__proto__:Array(0)

I want the output in a array with values as follows. 我想要具有以下值的数组中的输出。

[S&P, ex US, ex CL]

All values before the '-' are eliminated and values after '-' are taken and put in an array. 消除了“-”之前的所有值,并取“-”之后的值并将其放入数组中。 for eg '15- S&P' is changed to 'S&P'. 例如“ 15-S&P”更改为“ S&P”。 Can anyone please let me know how to achieve this. 任何人都可以让我知道如何实现这一目标。

You could use Array#map and String#split to return just the text after - sign from every element. 您可以使用Array#mapString#split从每个元素的-符号后仅返回文本。

 var arr = ["15- S&P", "us- ex US", "al- ex CL"], res = arr.map(v => v.split("- ")[1]); console.log(res); 

You may also try this; 您也可以尝试一下;

 var arr = ["15- S&P", "us- ex US", "al- ex CL"], res = arr.map(s => s.replace(/\\w+-\\s*([\\w&\\s]+)/,"$1")); console.log(res); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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