0

I have an object which looks like below

{
  "containers": [
    {
    "fields": [
        {},
        {},
        'associative1': {},
        'associative2': {}
      ] 
    }
  ]
}

So basically "fields" is an array which has elements like both fields[0], fields[1] & fields[associative1], fields[associative2]

My question is can I dynamically remove "just" the numbered elements... so finally 'fields' should just have associative1, associative2 elements

2
  • 4
    That array is not valid, you can't have named elements in an array literal. Commented Oct 14, 2017 at 6:39
  • 1
    Where and how you get this object, seems like it is not actually data. Commented Oct 14, 2017 at 7:03

3 Answers 3

1

Creating a valid object that looks like what it seems you intend...

var a = [{abc:1},{xyz:2}];
a.associative1 = {};
a.associative2 = {};
console.log(a);

Output (from Chrome)

(2) [{…}, {…}, associative1: {…}, associative2: {…}]
0: {abc: 1}
1: {xyz: 2}
associative1: {}
associative2: {}
length: 2

You can remove just the numbered array elements like this...

a.splice(0,a.length);

Or if the Array object is contained in a larger object something like this will work...

c.containers[0].fields.splice(0,c.containers[0].fields.length)
Sign up to request clarification or add additional context in comments.

4 Comments

Looks awesome...Just a quick question....What exactly is the type of "a" here & how does JavaScript allow assigning a.associative1 if It is declared as an array ...like it seems to be mixing types
@testndtv Javascript reports typeof a === 'object'. Arrays in Javascript have type 'object'. But, not every object in Javascript is an array. Array.isArray(a) === true. But Array.isArray({0:"hi"})===false. So you may want to check whether an object is an array before attempting to use Array methods like splice, that do not exist for non-Array objects.
@testndtv Recommend developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… The Description section speaks briefly about Javascript objects' associative array properties vs. Array elements.
Thx...Yes, I do see on the page why/how that syntax is allowed...But still wondering what "object property collection" of an array means (as referred on developer.mozilla.org/en-US/docs/Web/JavaScript/…)
1

Here you go with a solution

var data = {
  "containers": [
    {
    "fields": [
        {'abc': 1},
        {'xyz': 2},
        {'associative1': {}},
        {'associative2': {}}
    ]   
    }
  ]
};


for(var k in data["containers"][0]["fields"]){
 for(var key in data["containers"][0]["fields"][k]){
  if(!isNaN(data["containers"][0]["fields"][k][key])){
    delete data["containers"][0]["fields"][k];
  }
 }
}

data["containers"][0]["fields"] = data["containers"][0]["fields"].filter(function( element ) {
   return element !== undefined;
});

console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Your JSON is invalid. I've changed the JSON a little bit.

Hope this will help you.

1 Comment

@testndtv If you are satisfied please accept the answer.
0

It's an invalid object. The fields array include name properties, which is not allowed. Fields must be an object in that case. If test setting a variable to that object (just in a browser console) it will fail.

1 Comment

So I can reference fields[0], fields[1], fields[associative1], etc

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.