0

I have an array of objects with some properties as string values, can someone help me to get array of numbers out it. Here is the array looks like.

  scores = [
               {
                  maxScore:"100"
                  obtainedScore:"79"
                  passed:"pass"
                  subject:"Maths"
               },
               {
                  maxScore:"100"
                  obtainedScore:"73"
                  passed:"pass"
                  subject:"Science"
               },
               {
                  maxScore:"100"
                  obtainedScore:"82"
                  passed:"pass"
                  subject:"English"
               }
           ]

i want obtainedScore and maxScore should be taken out from these objects and place them in two different arrays

i tried this

for (var  i =0 ; i < score.length; i++)
{ 
   var marks[i] = parseInt(score[i].obtainedScore) ;
} 

and i found NaN.

5
  • 3
    As this is a pretty straightforward iteration of an array of objects, I'm wondering what code you have tried. Commented Aug 15, 2016 at 5:27
  • i tried this for (var i =0 ; i < score.length; i++){ var marks[i] = parseInt(score[i].obtainedScore; )} and i found NaN. Commented Aug 15, 2016 at 5:32
  • Put that code into your question. You should always show us what you have tried. Commented Aug 15, 2016 at 5:33
  • 1
    @GavisiddaGadagi probably that you refer to it as score when the declaration is scores. have a look at my answer which has a working snippet based on your attempt. Commented Aug 15, 2016 at 5:48
  • Cud Accept the answer that world and close the question if u solved the issue.. Commented Aug 15, 2016 at 7:02

4 Answers 4

1
  1. CORRECT ANSWER BASED ON YOUR ATTEMPT:

        var scores = [{
          maxScore: "100",
          obtainedScore: "79",
          passed: "pass",
          subject: "Maths"
        }, {
          maxScore: "100",
          obtainedScore: "73",
          passed: "pass",
          subject: "Science"
        }, {
          maxScore: "100",
          obtainedScore: "82",
          passed: "pass",
          subject: "English"
        }]
        var marks = [];
        for (var i = 0; i < scores.length; i++) {
          marks[i] = parseInt(scores[i].obtainedScore, 10);
        }
        console.log(marks)

  2. MY SOLN (from before you editted in your attempt)

var scores = [{
      maxScore: "100",
      obtainedScore: "79",
      passed: "pass",
      subject: "Maths"
    }, {
      maxScore: "100",
      obtainedScore: "73",
      passed: "pass",
      subject: "Science"
    }, {
      maxScore: "100",
      obtainedScore: "82",
      passed: "pass",
      subject: "English"
    }]

    function decoupler(arr, prop) {
      return arr.map(function(item, index) {
        return parseInt(item[prop], 10);
      });
    }
    var arr1 = decoupler(scores, "maxScore");
    var arr2 = decoupler(scores, "obtainedScore");

    console.log(arr1);
    console.log(arr2);

Edit: Added radix parameter for parseInt() based on comment by jfriend00.

Sign up to request clarification or add additional context in comments.

Comments

1

what about mapping a projection?

var maxScores = scores.map(score => parseInt(score.maxScore, 10))
var obtainedScores = scores.map(score => parseInt(score.obtainedScore, 10))

4 Comments

You need to convert from string to number. I like the general simplicity of the approach though.
Oh, of course. completely overlooked that. if we assume the string represents a number, than it should be sufficient to add a 'parseInt'
It's a good practice to include the radix argument when using parseInt(). Or you could just use score => +score.maxScore.
@jfriend00 thanx. wasn't aware of this. was just reading thru mdn. so actually old browsers before es5 used to read "010" as 8 bcoz of earlier default: 8. Strange!! I've edited my answer as well. with the radix parameter as 10.
0

I am not 100% sure what you want the output to be but :

i want obtainedScore and maxScore should be taken out from these objects and place them in two different arrays

var arrScore = [],
        arrMax = [];

    scores.forEach(i => {
        arrScore.push(!isNaN(parseInt(i.obtainedScore)) ? parseInt(i.obtainedScore) : 0);
        arrMax.push(!isNaN(parseInt(i.maxScore)) ? parseInt(i.maxScore) : 0);
    });

Basically this creates two arrays arrScore which will contain each individual score value and arrMax which contains an array of the max scores.

Using the forEach function we iterate the array and push the values into their respective arrays. Note for this we are also ensuring that the type is a valid integer.

Comments

0

scores = [
               {
                  maxScore:"100",
                  obtainedScore:"79",
                  passed:"pass",
                  subject:"Maths"
               },
               {
                  maxScore:"100",
                  obtainedScore:"73",
                  passed:"pass",
                  subject:"Science"
               },
               {
                  maxScore:"100",
                  obtainedScore:"82",
                  passed:"pass",
                  subject:"English"
               }
           ];
           maxScoreArray = new Array();
           obtainedScoreArray = new Array();

           for (var i = scores.length - 1; i >= 0; i--) {
           	maxScoreArray.push(Number(scores[i].maxScore));
           	obtainedScoreArray.push(Number(scores[i].obtainedScore));
           }

           console.log(maxScoreArray);
           console.log(obtainedScoreArray);

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.