How can I return one element from a large, multi-dimensional JavaScript array?

two dimensional array in javascript example code
javascript multidimensional array push
javascript multidimensional array push with keys
two dimensional array in javascript w3schools
javascript associative array
multiple array to single array in javascript
javascript multidimensional array with keys
find key in multidimensional array javascript

I have a huge array that I only need to return one particular value from, but I don't know how to access it. I only included a small section of it below. The part that I need to be able to access is at the very end, right before the function ("mag_id"). Usually, I can use some form of dot notation to transverse thru an array, but I haven't been able to find something that works in this case for some reason.

The full example is on JSBin: https://jsbin.com/xuzavud/edit?js,console,output

var array1=[{
    "AddtionalFields": [{
        "Name": "short_description",
        "Type": 4,
        "Value": "Made with a synthetic polymer..."
        },
        {
            "Name": "minimal_price",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "special_price",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "type_id",
            "Type": 9,
            "Value": "configurable"
        },
        {
            "Name": "Price",
            "Type": 7,
            "Value": "7.99"
        },
        {
            "Name": "absorbency_sort_integer_tf",
            "Type": 5,
            "Value": "0"
        },
        {
            "Name": "cost",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "giftcard_amounts",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "ACDesc",
            "Type": 9,
            "Value": "#E015 E015 3536P,3536,3537P,3537,3538P,3538 E015"
        },
        {
            "Name": "mag_id",
            "Type": 9,
            "Value": "12517"
        }
        ]
    }
]

function search(nameKey, myArray){
  for (var i=0; i < myArray.length; i++) {
    if (myArray[i].name === nameKey) {
        return myArray[i];
    }
 }
}

var resultObject = search("mag_id", array1);

console.log(resultObject);

your extract the item as below

var array1 = [
{"AddtionalFields":
[
{"Name":"short_description",
"Type":4,
"Value":"Made with a synthetic polymer, so they are less likely to cause irritation and allergic reactions.  The heavy-duty iris blue nitrile gloves feature textured fingers for improved grip. They meet or exceed ASTM D6319 and ASTM D6124 standards."},
{"Name":"minimal_price",
"Type":9,
"Value":""},
{"Name":"special_price",
"Type":9,
"Value":""},
{"Name":"type_id",
"Type":9,
"Value":"configurable"},
{"Name":"Price",
"Type":7,
"Value":"7.99"},
{"Name":"absorbency_sort_integer_tf",
"Type":5,
"Value":"0"},
{"Name":"cost",
"Type":9,
"Value":""},
{"Name":"giftcard_amounts",
"Type":9,
"Value":""},
{"Name":"ACDesc",
"Type":9,
"Value":"#E015 E015 3536P,3536,3537P,3537,3538P,3538 E015"},
{"Name":"mag_id",
"Type":9,
"Value":"12517"}] } ];

const _item = array1[0].AddtionalFields.find((el)=>{
return el.Name == 'mag_id'
})

console.log(_item)

Learn JavaScript Multidimensional Array By Examples, how to access array of objects in javascript javascript foreach multidimensional array. I have a huge array that I only need to return one particular value from, but​  To access an element of the multidimensional array, you first use square brackets to access an element of the outer array that returns an inner array; and use another square bracket to access the element of the inner array. The following example returns the second element of the first inner array in the activities array above.

Based on your array it is not multidimensional and has one object element. First you need to extract the keys and values of your object. Then you get the length, loop and filter through it. The code below returns the object where your keyword was found. You can also view more information here about looping through objects. Hope this helps.

    var obj=[{
        "AddtionalFields": [{
            "Name": "short_description",
            "Type": 4,
            "Value": "Made with a synthetic polymer..."
        },
        {
            "Name": "minimal_price",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "special_price",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "type_id",
            "Type": 9,
            "Value": "configurable"
        },
        {
            "Name": "Price",
            "Type": 7,
            "Value": "7.99"
        },
        {
            "Name": "absorbency_sort_integer_tf",
            "Type": 5,
            "Value": "0"
        },
        {
            "Name": "cost",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "giftcard_amounts",
            "Type": 9,
            "Value": ""
        },
        {
            "Name": "ACDesc",
            "Type": 9,
            "Value": "#E015 E015 3536P,3536,3537P,3537,3538P,3538 E015"
        },
        {
            "Name": "mag_id",
            "Type": 9,
            "Value": "12517"
        }
        ]
    }
    ]

    function search(nameKey, myArray){

        //Extract values from object
        var values = Object.values(myArray); 

        //loop through the object and filter the keyword
        for(var x of values){
            for(var i = 0; i < Object.keys(x.AddtionalFields).length; i++){
                if(x.AddtionalFields[i].Name == nameKey || x.AddtionalFields[i].Type == nameKey || x.AddtionalFields[i].Value == nameKey){
                    return x.AddtionalFields[i];
                }
            }
        }

    }

    var resultObject = search("#E015 E015 3536P,3536,3537P,3537,3538P,3538 E015", obj);

    console.log(resultObject);

Arrays, However, you can create a multidimensional array by defining an array of elements, where each element is also another array. For this reason, we can say that a  To return a value in a multidimensional array: array [n] [m] is the mth element of the nth row. To do this for every element, use embedded for-loops: for (var i = 0; i < array.length; i++) { for (var j = 0; j < array[i].length; j++) { console.log(array[i] [j]); share.

Efficient way to search a multi-dimensional array for key based on , There are two syntaxes for creating an empty array: For instance, a single element with a large index gives a big length: arr ) ; // [1, 2] arr . length = 5 ; // return length back alert ( arr [ 3 ] ) ; // undefined: the values do not return We can use it for multidimensional arrays, for example to store matrices:. shift() removes the first element from an array and returns that element. let myArray = new Array('1', '2', '3') let first = myArray.shift() // myArray is now ["2", "3"], first is "1" unshift() adds one or more elements to the front of an array and returns the new length of the array.

How to create two dimensional array in JavaScript?, I am trying to write a function (that is fast and efficient) that will return the key (or array of keys if there are more than 1) of the outer-most array based on a  Accessing Multi Dimensional Array Elements. In Java programming, We can use the index position to access the Java multi dimensional array elements. Using the index, we can access or alter/change every individual element present in the multi dimensional array. The index value of Multi Dimensional Array in Java starts at 0.

Multidimensional Arrays in Java, The two-dimensional array is an array of arrays, so we create an array of <​script>. // Create one dimensional array Loop to display the elements of 2D array. Internally, the filter () method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array. The filter () method accepts two named arguments: a callback function and an optional object.

count - Manual, Data in multidimensional arrays are stored in tabular form (in row major order). The above example represents the element present in first row and first column  To loop over the elements of the array: for (let i=0; i<arr.length; i++) – works fastest, old-browser-compatible. for (let item of arr) – the modern syntax for items only, for (let i in arr) – never use. We will return to arrays and study more methods to add, remove, extract elements and sort arrays in the chapter Array methods.

Comments
  • What mulit-dimensional array? array1 has only one dimension, and its single element is an object.
  • @Ipradhap Thanks for your help. That was exactly what I needed to get the right information.