2
data = {
      "test": {
    
    "questions": [
        {
            "option1Id": "1001",
            "option2Id": "1002",
            "tags": [],
            "_id": "60e57c069107a038085ae3a1",
            "question": "what is the atomic number of oxygen?",
            "option1": "5",
            "option2": "6",
            "option3": "7",
            "option4": "8",
            "explanation": "The atomic number of oxygen is 8 and the mass number of oxygen is 16.",
            "level": "Easy",
            "option3Id": "1003",
            "option4Id": "1004"
                }]
              }
    }

I want to achieve something like this:

[{ option1: '5', option1Id: '1001'}, 
 {option2: '6', option2Id: '1002'}, 
 {option3: '7', option3Id: '1003'}, 
 {option4: '8', option4Id: '1004'}] 

Right Now, I'm trying to separate option1Id, option2Id, option3Id, and option4Id from option1, option2, option3, option4.

This is what I've done until now:

data = {
  test: {
    questions: [
      {
        option1Id: '1001',
        option2Id: '1002',
        tags: [],
        _id: '60e57c069107a038085ae3a1',
        question: 'what is the atomic number of oxygen?',
        option1: '5',
        option2: '6',
        option3: '7',
        option4: '8',
        explanation:
          'The atomic number of oxygen is 8 and the mass number of oxygen is 16.',
        level: 'Easy',
        option3Id: '1003',
        option4Id: '1004',
      },
    ],
  },
};

options = [];
// console.log(Object.keys(data.test.questions[0]))
entries = Object.entries(data.test.questions[0]);
for (let i = 0; i < entries.length; i++) {
  // options.push(entries[i][0].filter(e => (e.length = 6) && (e.includes('option'))));
  if (entries[i][0].includes('option')) options.push(entries[i]);
}
console.log(options, 'First Options');

for (let i = 0; i < options.length; i++) {
  if (options[i][0].includes('Id')) options.splice(options[i], 1);
   }

console.log(options, 'Second options');

As you can see, I can't seem to get rid of option3Id and option4Id. Is there any way I can do that? And after that how do I convert it into my desired form?

1 Answer 1

2

You could use a regex test instead of checking if the name has option in it:

data = {
  test: {
    questions: [{
      option1Id: '1001',
      option2Id: '1002',
      tags: [],
      _id: '60e57c069107a038085ae3a1',
      question: 'what is the atomic number of oxygen?',
      option1: '5',
      option2: '6',
      option3: '7',
      option4: '8',
      explanation: 'The atomic number of oxygen is 8 and the mass number of oxygen is 16.',
      level: 'Easy',
      option3Id: '1003',
      option4Id: '1004',
    }, ],
  },
};

options = [];
let reg = /option\d$/
entries = Object.entries(data.test.questions[0]);

for (let i = 0; i < entries.length; i++) {

  if (reg.test(entries[i][0])) {
    let tmpObj = {};
    let idVersion = entries.find(el => el[0] === (entries[i][0] + 'Id'));
    
    tmpObj[entries[i][0]] = entries[i][1];
    tmpObj[idVersion[0]] = idVersion[1];
    
    options.push(tmpObj);
  }
}
console.log(options, 'First Options');

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

6 Comments

I think you almost finish it. It would be better to output an expected result OP wants.
@ikhvjs thanks, read his question not totally, thanks for pointing it out. Updated answer
Thank you so much for the solution! Could you please also explain to me why the splice() method doesn't work as I was expecting it to be? I mean why do option3Id and option4Id remain while option1Id and option2Id don't?
Also, could you explain to me what if (reg.test(entries[i][0])) does exactly?
your object.entries returns arrays with first element the key of the obj and second el the val, I take the key from it and test if it passes the regex so I know its a key in the value of option+number developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/…
|

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.