1

I am just a beginner in JavaScript, I have issue how to retrieve data from this array. Is it valid? Here is console output of array object:

[]
0: Item {id: 0, symbol: "VBIV", boughtDate: "2018-07-22", company: "VBI Vaccines Inc."}
1: Item {id: 1, symbol: "R", boughtDate: "2018-07-22", company: "Ryder System Inc."}
2: Item {id: 2, symbol: "R", boughtDate: "2018-07-22", company: "Ryder System Inc."}
length: 3
__proto__: Array(0)
jacob3143
  • 11
  • 2

3 Answers3

2

If you know the index of the object you want to retrieve you will simply use array[index].

If you do not know the index but you know the id you will use:

array.find(element => element.id === id)
mosmk
  • 403
  • 2
  • 7
-1

For example I want to get id:2 company name.

To get this value, you would get the object at index 2 of the array using the bracket [] syntax, and get the id property from that object using the dot . syntax.

console(myArray[2].id) //=> 2
console(myArray[2].company) //=> "Ryder System Inc."

Or save the object from the array to a variable, and then get it's properties:

var myObj = myArray[2]
console(myObj.id) //=> 2
console(myObj.company) //=> "Ryder System Inc."
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
-2

I glad to know if this could be faster than the existing, and match to what you wants.

Array.prototype.get = function(key, value, start = 0, revert = false, restart = true) {
    let __forward = function(index, limit, array, key, value) {
        let ele = null;
        while (index < limit && (ele = array[index])[key] !== value) index++;
        return {index: index, value: ele};
    },
    __backward =function(index, limit, array, key, value) {
        let ele = null;
        while (index > limit && (ele = array[index])[key] !== value) index--;
        return {index: index, value: ele};
    };

    if (!(typeof key !== "string" || start < 0 || !(start < this.length) || typeof this[0] !== "object")) {
        let length = this.length, result; 

        /* no revert: forward direction, until first match */
        if (!revert) {
            result = __forward(start, length, this, key, value);

            if (result.index < length)
                return result;

            else if (restart && start > 0) {
                result = __forward(0, start, this, key, value);

                if (result.index < start)
                    return result;
            }

        } else {

        /* revert: backward direction, until last match */

            result = __backward(start, -1, this, key, value);

            if (result.index > -1)
                return result;

            else if (restart && start < length-1) {
                result = __backward(length-1, start, this, key, value, true);

                if (result.index > start)
                    return result;
            }
        }
    }

    return {index: -1, value: null};
}

usage:

 let a = [{id: 1, value: "rock"}, {id: 2, value: "roll"}, ...];

 let item2 = a.get("id", 2, a.length-1, true),
     val2 = item2.value,
     idx2 = item2.index;

 let item1 = a.get("id", 1, idx2 - 1, true),
     val1 = item1.value,
     idx1 = item1.index;

 etc...

As a regular function:

window.OO7array_get = function(key, value, start = 0, revert = false, restart = true) {

    /*  same as above script, dont worry feel free to copy and paste here */

};
OO7
  • 660
  • 4
  • 10
  • 1
    a) adding to built in types is almost always a bad idea. b) there's already built-in functionality that does what this function does. – Heretic Monkey Mar 07 '20 at 00:17
  • I dont think so. Because it has capability to be extend, however should be consider that its depending on how the extension been organized and how it could help important improvement without unnecessary influence. – OO7 Mar 07 '20 at 00:29