1

I have following foobar variable which is string(I get it like so by querying a webserver with $http.post):

var foobar = "[{param1: 'value1'}, {param2: 'value2'}]";

How do I make an array out of that? If I use JSON.parse, it will provide me with an object(tried typeof foobar). But, if I then use delete param1, it will then make it [null, {param2: 'value2'}]. What I want to do, basically is to remove first or second item. Also, value may be different, but keys will always stay the same, ie. they'll be numbers.

17
  • I don't understand your question - the variable foobar is an array, with two elements that are each objects. Your delete param1 can't even work because that's a key of the 0th inner object. Commented Nov 16, 2016 at 23:25
  • did you mean var foobar = "[{param1: 'value1'}, {param2: 'value2'}]";? Commented Nov 16, 2016 at 23:27
  • Yes, sorry, I forgot to wrap it under double-quotes, Commented Nov 16, 2016 at 23:30
  • And now it's not a valid JSON. Commented Nov 16, 2016 at 23:35
  • 1
    @Milos well, it really makes very little sense to not fix it first. Commented Nov 16, 2016 at 23:52

5 Answers 5

2

Seems like you're wanting to parse your array into a single object, from your delete statement. If so...

var foobar = '[{"param1":"value1"},{"param2":"value2"}]';
var json = JSON.parse(foobar);

json = json.reduce(function(previousValue, currentValue){
  Object.keys(currentValue).forEach(function(key){
    previousValue[key] = currentValue[key];
  });
  return previousValue;
}, {});

console.log(json);

I could be wrong though.

Sign up to request clarification or add additional context in comments.

4 Comments

json.reduce((a, o) => Object.assign(a, o), {}) :-)
Ohh, fancy. :D Ya, i'm not up on ES6 fully yet.
Btw, also note that OP has a broken JSON and apparently they are not willing to fix it. So presumably JSON.parse would not work.
Lol, well. shrug
0

Description

This is already an array var foobar = [{param1: 'value1'}, {param2: 'value2'}];.

Example

var foobar = [{param1: 'value1'}, {param2: 'value2'}];
// this is an array
console.log(foobar);
// this is an object in an array
console.log(foobar[0]);

Comments

0

This

var foobar = [{param1: 'value1'}, {param2: 'value2'}];

is an array.

var foobar = [{param1: 'value1'}, {param2: 'value2'}];
console.log(foobar instanceof Array)

If you want to remove the first item you can just use the shift method:

var foobar = [{param1: 'value1'}, {param2: 'value2'}];
foobar.shift();
console.log(foobar)

While if you want to remove the second item you can use the pop method:

var foobar = [{param1: 'value1'}, {param2: 'value2'}];
foobar.pop();
console.log(foobar)

Comments

0

As the others say, what you are providing is already an array.

var foobar = [{param1: 'value1'}, {param2: 'value2'}];
//You can see this because of the []
//You can delete the index of an array by using the splice method
foobar.splice(1,1);
//First param is the index, the second is the number of items to erase

Comments

0

What's probably confusing you the most is typeof.

In the case of arrays, typeof will say it is an object (as you can read from MDN here). If you want to test to see if a variable is an array, you could use: foobar.isArray()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.