1

Stuck in a situation where i tried different solution but didn't get any workaround what i want is:

$scope.list={"A":[],"B":[],"C":[],"D":[]}

this type of objects of array but its should be dynamic for example when i click button this function will be called:

AppendToList(data)
{
       $scope.list.push("E":data)//error
}

i want this E to be dynamic when ever i click the next object should be created automatically if I click next time it should become {"A":[],"B":[],"C":[],"D":[],"E":[data]}

2
  • 2
    It is not valid javascript; You can do $scope.list["E"] = data Commented Jun 23, 2017 at 8:53
  • list isn't an array so you can't push items into it. Also note that the order of the items isn't guaranteed. Commented Jun 23, 2017 at 8:53

4 Answers 4

2

You should try like this.

AppendToList(data)
{
       $scope.list["E"]=data;
}
Sign up to request clarification or add additional context in comments.

3 Comments

but every time I want a dynamic list like next time is should be F
if i do something like this $scope.list[data]=data; it is working thanks bro :p
AppendToList(data,key) { $scope.list[key]=data; }
1

Your $scope.list is actually an object. To add a property you need to use list.A or list["A"]

var list = {};
list["A"] = [1,2,3];
console.log(JSON.stringify(list));

Comments

0

Try this way to add new object as dynamic element

  
   var key = "E";
    var value= { Result: 'Very Success' }
    var list = { "A": [], "B": [], "C": [], "D": [] };
    var splited = JSON.stringify(list).split('}');
    list = JSON.parse(splited[0] + ',"' + key +' ":'+ '[' + JSON.stringify(value)+ '] }')
    console.log(list);

Comments

0

try this

function nextChar(c) {
    var i = (parseInt(c, 36) + 1 ) % 36;
    return (!i * 10 + i).toString(36);
}

AppendToList(data)
{
$scope.list[nextChar($scope.list[Object.keys($scope.list)[Object.keys($scope.list).length - 1]])]=data;
}

tested it and its working fine you have just to add condition id the list is empty of full

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.