1

So I have an array like this var arr = [foo1:"bar1", foo2:"bar", foo3:"bar3"].

Then I used this arr.splice(index, 1); given that the index is dynamic lets just say it's 0 but the value foo1:"bar1" wasn't removed from the array.

Also the above array works on my code but it shows an error on JSFiddle. I have no idea why.

Thanks.

EDIT I dont know why it doesn't show an error on my console but here's how I did it.

var arr = [];

I dynamically added value to the array by doing

arr["foo"] = "bar";

which resulted to [foo:"bar"];

Any idea guys?

EDIT 2

Here's the screenshot of the console. enter image description here

8
  • That doesn't look right... You have an array or an object? Commented Jan 12, 2016 at 5:56
  • That isn't a valid array. We can't help without some valid structure Commented Jan 12, 2016 at 5:56
  • 1
    This will give you an error.If you want to create a JSON array you should write like var arr = [{foo1:"bar1"},{ foo2:"bar"}, {foo3:"bar3"}]; Commented Jan 12, 2016 at 5:56
  • Your array is not an valid array then how come Also the above array works on my code ? Commented Jan 12, 2016 at 5:57
  • This is more of an object structure (if you replace square brackets with curly braces), rather than array. if you try to instantiate var arr = [foo1:"bar1", foo2:"bar", foo3:"bar3"], it will give you an error. Commented Jan 12, 2016 at 5:59

2 Answers 2

3

Structure of this array is not valid.

var arr = [foo1:"bar1", foo2:"bar", foo3:"bar3"];

An array will have comma-separated items within square braces [], not key values

If you want to have key values, then it should be Object with curly braces {}

var arr = {foo1:"bar1", foo2:"bar", foo3:"bar3"};

and if you want to remove first property from the arr, then do

var keys = Object.keys( arr );
delete arr[ keys[ 0 ] ];

For adding a key value,

arr[ "foo5" ] = "bar";
Sign up to request clarification or add additional context in comments.

6 Comments

there really is no reliable first since objects have no order
@charlietfl it will have no defined order if the object is constructed at runtime. if the object is defined at compile time, then the defined order will be maintained. try var arr = {foo4:"bar1", foo2:"bar", foo3:"bar3"}; and then delete arr[ Object.keys( arr )[ 0 ] ]
But how do I dynamically add a key:value pair on that format in an object?
@TheGreenFoxx arr[ "foo5" ] = "bar"; should add or overwrite foo5 property into arr
Thanks man. This helped me a lot. The only thing that got me stuck is that the console seems to accept that array as valid. I got confused.
|
1

Long way:

var arr = [];

var foo1 = {};
foo1['foo1'] = 'bar1';

var foo2 = {};
foo1['foo2'] = 'bar2';

arr.push(foo1);
arr.push(foo2);

alert(arr.length); // output: 2

var index = arr.indexOf(arr, foo1); // or var index = $.inArray(arr, foo1);
arr.splice(index, 1);

alert(arr.length) // output: 1

2 Comments

how did jQuery get into this?
Thanks Happy but this seems to be not dynamic.

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.