-2

I have an array :

itemSku = ["MY_SERVICE","SKU_A","SKU_B"];

Now I'm passing this value to a component in Angular. There I check the type of the itemSku and it is returning a string, so I am not able to perform array operations on this.

console.log(typeof itemSku);
*string*

How can I convert this back to an array? I'm not allowed to use jquery but can use Javascript, Ramda or Lodash

pkdq
  • 191
  • 1
  • 14

3 Answers3

0

You can use JSON.parse(itemSKu) to convert it back to array

Komal Bansal
  • 789
  • 2
  • 7
  • 20
  • 1
    should be a comment. – Jai Dec 12 '18 at 06:47
  • 3
    @Jai why? It seems like a legitimate answer. It could be improved, sure, and it suffers because the *question* seems to be missing information but it does attempt to answer it. – VLAZ Dec 12 '18 at 07:08
  • @vlaz Definitely. But answering in one line without even asking about what are the other issues, just throw the answer is not good as per SO standards. If one has one liner better to add that as a comment. Yet i commented just for her to improve it. – Jai Dec 12 '18 at 07:34
  • @Jai seems that users with rep < 50 can't comment – barbsan Dec 12 '18 at 07:40
0

try this:

let result = itemSku.split(",");
Kumar Adarsh
  • 154
  • 5
0

We definitely need more context to answer this question. However, it may be useful to know that arrays can be converted to strings in some circumstances and perhaps you put yourself in that situation by accident:

Note that the default character used for joining the array is a comma ,:

var a = ['foo', 'bar'];

console.log(a.toString());
console.log(a + '');
console.log('' + a);
console.log(a.join());
console.log(String(a));

To recover from that, you don't really need Ramda, you could do it in plain JavaScript:

var b = 'foo,bar';
var a = b.split(',');

console.log(a);

If you really wanted Ramda, R.split would do a similar job:

var splitByComma = R.split(',');
var a = splitByComma('foo,bar');
console.log(a);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.min.js"></script>

References

  1. String.prototype.split
  2. R.split
customcommander
  • 17,580
  • 5
  • 58
  • 84