Mat.Now
Mat.Now

Reputation: 1725

Convert object to array, insert value between array indexes

I have one question I send form from client to server and get object which checkbox is true and here is problem. e.g. '0': true, '3':true and I would like that my array looks [true,false,false,true], but now I get array like this [true, true] I use map. So in short between object true i would insert false. Please look on my code:

var correctAns = [];
var quizCorrectAnswer = 
    { '0': { '0': true },
      '1': { '0': true, '3': true },
      '2': { '2': true } 
     }
for(correct in quizCorrectAnswer){
  correctAns.push(Object.keys(quizCorrectAnswer[correct]).map((k) => 
  quizCorrectAnswer[correct][k]))
}

I would like receive following arrays: 0: [true], 1: [true, false, false, true], 2: [false, false, true] but now I get 0: [true], 1: [true, true], 2: [true]

Upvotes: 0

Views: 61

Answers (4)

Blue
Blue

Reputation: 22911

This should be what you're looking for:

var correctAns = {};
var quizCorrectAnswer = 
    { '0': { '0': true },
      '1': { '0': true, '3': true },
      '2': { '2': true } 
     }
     
Object.keys(quizCorrectAnswer).forEach((question) => {
  correctAns[question] = Object.keys(quizCorrectAnswer[question]).reduce((prev, key) => {
    while (prev.length < key) {
      prev.push(false);
    }
    prev.push(true);
    return prev;
  }, []);
});

console.log(correctAns);

If you want correctAns to simply be an array, use this instead:

var quizCorrectAnswer = 
    { '0': { '0': true },
      '1': { '0': true, '3': true },
      '2': { '2': true } 
     }
     
var correctAns = Object.keys(quizCorrectAnswer).map((question) => {
  return Object.keys(quizCorrectAnswer[question]).reduce((prev, key) => {
    while (prev.length < key) {
      prev.push(false);
    }
    prev.push(true);
    return prev;
  }, []);
});

console.log(correctAns);

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138267

  const result = {};
  for(const [key, obj] of Object.entries(quizCorrectAnswer))
     result[key] = Array.from({...obj, length:5}, el => !!el);

You could use Array.from to convert the object into an array. Instead of the static length of 5, you could also take Object.keys(obj).length

Upvotes: 0

Mikhail Zharnikov
Mikhail Zharnikov

Reputation: 178

That's the problem of non-appeared keys and values. Here in quizCorrectAnswer variable value about key '1', there are only '0' and '3' keys. No '1' or '2' key and value is defined in that value. And Javascript map function matches the existing keys to values and so you can get only [true, true] about '1' key (because there are only 2 keys in that value). So to get the result you want, you have to change the quizCorrectAnswer value as follows.

var quizCorrectAnswer = 
    { '0': { '0': true },
      '1': { '0': true, '1': false, '2': false, '3': true },
      '2': { '0': false, '1': false, '2': true}
    };

Upvotes: -1

Nina Scholz
Nina Scholz

Reputation: 386570

The problem is, you map the keys which return an array with only the given answers. But you need an array with the holes and their false values.

To iterate a sparse array with an array method, you need to convert it into an array without sparse elements and then map a boolean value.

var quizCorrectAnswer = { 0: { 0: true }, 1: { 0: true, 3: true }, 2: { 2: true } },
    correctAns = [],
    correct,
    temp;
    
for (correct in quizCorrectAnswer) {
    temp = [];

    Object
        .keys(quizCorrectAnswer[correct])
        .forEach(k => temp[k] = quizCorrectAnswer[correct][k])

    correctAns.push(Array.apply(null, temp).map(Boolean));
}

console.log(correctAns);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 1

Related Questions