0

Basically I was trying to access the properties of an object in JavaScript and than I thought If i can access the array values present inside the object

// Object
let user={
  name:"rajat",
  age:23,
  email:["ag.112","ag.113"]
};

now for accessing particular elements of an array I am not able to understand how to do this

console.log(user);
console.log(user.name);
console.log(user.age);
console.log(user['age']);

like I want to access the 2nd element present inside the email(ag.113) In JavaScript

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
Rajat Agrawal
  • 141
  • 1
  • 4
  • 13
  • 3
    Try `user.email[1]` for that. Objects are key-value pair, so dot(`.`) makes sence horever, arrays are indexed and hence you will have to use `position` of element where first position is 0 – Rajesh Dec 18 '19 at 11:15
  • 1
    I have removed the [tag:java] tag. Please note that Java and Javascript are as different as Austria and Australia. – RealSkeptic Dec 18 '19 at 11:21
  • You can post your comment as an answer It was enough than I can mark it as an answer and thanks for clearing my doubt I just started learning JavaScript @Rajesh – Rajat Agrawal Dec 18 '19 at 11:21
  • You need to select property first and then select element which you need (array iteration started from 0) `user.email[1]` – demkovych Dec 18 '19 at 11:22
  • You can write a simple callback function --> let user={ name:"rajat", age:23, email:["ag.112","ag.113"] }; user.email.forEach(function(arr) { console.log(arr); }); – DESH Dec 18 '19 at 11:32

1 Answers1

0

I don't know if that's what you mean, but you can do :

numb = 0/1

user.email[numb]

to get the contents of "email"; or you can do

user[email][numb]

i wrote a simple function to handle those easy and putting them into local Storage:

function getlist(option, name, listid, must, childnam, contenta, contentb) {

    if (list != 1) {
        if (option != "help") {
            var listid = "produkte";
            var list = JSON.parse(localStorage.getItem(listid));



            console.log(list)

            var allleng = _.size(list);



            console.log(cont);
            switch (option) {
                default: var child = list;

                return child;
                break;
                case "get":
                        var child = list[name];

                    return child;
                    break;
                case "pardel":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    delete list[name];
                    localStorage.setItem(listid, JSON.stringify(list));
                    return list;
                    break;

                case "setpar":

                        list = _.extend(list, _.object([
                        [contenta, {}]
                    ]));
                    localStorage.setItem(listid, JSON.stringify(list));
                    return list;

                    break;
                case "set":

                        list = _.extend(list, _.extend(list[name], _.object([
                        [childnam, contenta],
                        [(childnam + "must"), must]
                    ])));

                    localStorage.setItem(listid, JSON.stringify(list));
                    return list;
                    break;
                case "setchild":

                        list[name] = _.extend(list, _.extend(list[name], _.object([
                        [childnam, _.object([
                            [contenta, contentb],
                            [(contenta + "must"), must]
                        ])]
                    ])));

                    localStorage.setItem(listid, JSON.stringify(list));
                    return list;
                    break;
                case "length":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    return leng;
                    break;
                case "parlength":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    return _.size(list);
                    break;
                case "childlength":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    return _.size(list[name][childnam]);
                    break;
                case "childdel":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    delete list[name][childnam][contenta];
                    localStorage.setItem(listid, JSON.stringify(list));

                    return list;
                    break;
                case "del":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    delete list[name][childnam];
                    localStorage.setItem(listid, JSON.stringify(list));

                    return list;
                    break;
                case "getchild":
                        var child = list[name];
                    var leng = _.size(child);
                    var cont = child[childnam];
                    return list[name][childnam];
                    break;
                case "getpar":

                        return list;
                    break;

            }
        } else {
            console.log("getpar: gibt alle Arten an Produkten aus");
            console.log("get: gibt den ersten inhalt aus");
            console.log("getchild:");
            console.log("pardel:");
            console.log("del:");
            console.log("set:");
            console.log("setchild:");
            console.log("setpar:");
            console.log("parlength:");
            console.log("childlength:");
            console.log("length:");
            console.log("help: hilfe!! lasst mich hier raus!!!");
        }


    } else {}
}