1

enter image description here

 var new_data = $.parseJSON(data);
 for (var j = 0; j < new_data.all_soa_details.length; j++) {
 var td7_contact = new_data.all_soa_details[j].bal; //100,200,200,50 . here i need to fecth 50 only
 let td77_contact = Math.abs(td7_contact);
 }

i am getting values 100, 200,40,60, 50.. Here i need to fetch 50 becoz its last valuje in the for loop .How to get this last value?

DevZer0
13.5k7 gold badges29 silver badges54 bronze badges
asked Nov 13, 2021 at 7:06
8
  • td7_contact isn't an array, why are you trying to iterate over it? Commented Nov 13, 2021 at 7:18
  • Can you show what data looks like? What is the value of the .bal property? Commented Nov 13, 2021 at 7:26
  • yes its not an array.. but i want to fetch the last value as 50 ..Need to show balance after the loop Commented Nov 13, 2021 at 7:26
  • Is it a comma-separated string? Commented Nov 13, 2021 at 7:27
  • After the loop is done, td7_contact contains the last value. Commented Nov 13, 2021 at 7:30

2 Answers 2

1
new_data.all_soa_details[new_data.all_soa_details.length - 1].bal

should give you the expected result

answered Nov 13, 2021 at 7:31
Sign up to request clarification or add additional context in comments.

Comments

0

Try this out, I just wrote a code for you..

let new_data = {
 all_soa_details: [
 {
 bal: [1, 2, 3, 45],
 },
 {
 bal: [1, 2, 3, 45],
 },
 ],
 };
 for (var j = 0; j < new_data.all_soa_details.length; j++) {
 var td7_contact = new_data.all_soa_details[j].bal; //100,200,200,50 . here i need to fecth 50 only
 let td77_contact = Math.abs(td7_contact);
 }
 // let findOut;
 // for (const lastIter of td7_contact) {
 // findOut = lastIter; //Last iteration value stored in each iteration.
 // }
 // console.log(findOut);
 // Main code ....
 console.log(
 new_data.all_soa_details[0].bal[
 new_data.all_soa_details[0].bal.length - 1
 ]
 );
 for (let i = 0; i < new_data.all_soa_details.length; i++) {
 console.log(
 new_data.all_soa_details[i].bal[
 new_data.all_soa_details[i].bal.length - 1
 ]
 );
 }
answered Nov 13, 2021 at 7:17

2 Comments

I don't think bal is an array.
@NimwNs You already said that above, you don't have to tell me twice.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.