How to Append My Data in Nested Object

How i can append my data in nested Object?

You have a JavaScript object, not a list or an array so you can't push to it. You can however, define a new key with it's associated value like so:

users[accountNumber] = {
name,
address,
accountType: type,
deposits: 0,
withdraws: 0,
balance: 0
};

And if you wanted to remove an object you could do

delete users[accountNumber]

It's important to remember that JavaScript objects like yours effectively act as a dictionary (key/value pairs), where an array is just a list. If you wanted the behaviour of a list then you could change your object definition slightly to look like:

let users = [
{
accountNumber: '123',
otherFields: ''
}
];

users.push({accountNumber: '1234', otherFields: 'test'});

However then you would lose the ability to directly access the object by the account number, it all depends what best suits your requirements.

Append JSON Nested Object

What I did to fix this was let:

for (var i = 0; i < data.length; i++) {
var currData = data[i];
listContent += '<li data-weekOf="'+ currData.WeekOf+'">';
if(currData.Team !== null){
listContent += '<h2>' + currData.Team.results.join(', ') +'</h2>';
}else{
listContent += '<h2>' + "Null" +'</h2>';
}

How can I take a group of nested JSON objects and append their data to an editable text field?

You can use map method only for arrays.
For iterate through object, you can use

Object.keys(questions).map(function(key, index) {
//do something here
})

Otherwise you can use a for cycle but i dislike it

for (var key in questions) {
if (questions.hasOwnProperty(key)) {
// use questions[key]
}
}

how to form the nested object in Formdata in javascript

It should be:

data.append("address[0]['state']", "ewrwer");
data.append("address[0]['city']", "asdasd");

Because, it is inside array of index 0, then inside that you have addresses.

How to append nested-object in RealmSwift?

You're going through too many steps with saving the Realm so let's simplify!

Lets set this up with a PersonClass that has dogs

class PersonClass: Object {
@objc dynamic var name = ""
list dogList = List<DogClass>()
}

and then a DogClass which uses linkingObject for a path back to it's owner(s)

class DogClass: Object {
@objc dynamic var name = ""
let linkingPerson = LinkingObjects(fromType: PersonClass.self, property: "dogList")

and then instantiate a person and a dog and add the dog the persons List

let person = PersonClass()
person.name = "Jay"

let dog = DogClass()
dog.name = "Rover"

person.dogList.append(dog)

let realm = try! Realm()
try! realm.write {
realm.add(person) //this add the person *and* the dog to Realm
}

Then to address your question, suppose later on our person gets a new dog

let's load the person

let realm = try Realm()
let person = realm.objects(PersonClass.self).filter("name == 'Jay'").first!

then create the dog

let anotherDog = DogClass()
anotherDog.name = "Spot"

then, and this is the important bit, add the dog to the person within the write, which will add the dog to Realm as well as adding it to the persons list - all in one statement.

try! realm.write {
person.dogList.append(anotherDog)
}

Note you should not do this .first! but I did it for brevity in this answer. You should always safely handle and unwrap optionals.



Related Topics



Leave a reply



Submit