1

Can't really figure out why I cant get the values that I have passed through an AJAX call in my javascript function.

As seen in the picture, msg is the array that's passed from the backend to the javascript function I've tried both

info['taxarray'].forEach() and info.taxarray.forEach() 

Console.log(info.taxarray) works fine and it outputs the array in the console, but when I try to run iterations through this array, I can't access the values in it, I'm sure I'm missing something, but I can't figure out what it is.

This is the output for the console

3
  • What have you tried to debug the problem? Please share all information in text form. Also, please explain how this is related to PHP Commented Mar 18, 2021 at 20:39
  • info.taxarray.forEach(taxArr => { taxArr.forEach(tax=> console.log(tax.tax_values)) }). taxarray is an array of arrays as sohaieb said. Commented Mar 18, 2021 at 20:52
  • Please avoid pictures of code. They can't be copy-pasted. Use code formatting instead. Commented Mar 18, 2021 at 22:03

3 Answers 3

5

You can flatten the multidimensional array with flat method:

info.flat().forEach(item => console.log(item.tax))

Using flatmap it gets even more straightforward

info.flatMap(item => console.log(item.tax))
Sign up to request clarification or add additional context in comments.

2 Comments

what would be the difference between those two? how would the output would look for each one. I implemented flat() so it looks like info.taxarray.flat().forEach(function (taxes){ console.log(taxes.tax); });
3

As i can see, your info.taxarray is a multidementional array so that's every element inside it is also an another array.
your console.log(info.taxarray) gives see :

[Array(1), Array(1)]

to access tax_values of the first element you have to write info.taxarray[0][0].tax_values so that means you have to iterate the taxarray and also it's elements (which are arrays too) .
Check your code. you can also refer to the solution of Mike Ezzati it help a lot

1 Comment

God that was a stupid mistake by me, THANK YOU !
1

the response you get seems to be multidimensional (an array of arrays each of which has only one element)

info.taxarray.forEach(msg) { (item) => {
    console.log(item[0].tax_values)
}};

OR if array elements can be more so iterate again

info.taxarray.forEach(msg) { (item) => {
    item.forEach(msg) { (item) => {
        console.log(i.tax_values)
    }};
}};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.