0
var person = {
          course1:{Name:"xxx", day:"mon", time:"2am-6pm"},
          course2:{Name:"yyy", day:"tue", time:"7am-9pm"},
          course3:{Name:"zzz", day:"tue", time:"2am-6pm"},
          course4:{Name:"aaa", day:"wed", time:"2am-6pm"},
          course5:{Name:"bbb", day:"thu", time:"2am-6pm"},
          course6:{Name:"ccc", day:"mon", time:"2am-6pm"} 
        };

Can anyone please show how to loop and iterate through this person object and swap the course1 and course2 object values(name, day, time) and do something, replace it and swap course1 and course3 again do something and so on...

7
  • 3
    I'm not sure I understand, what is it that you want to achieve at the end of the process? What have you tried so far? Commented Mar 31, 2018 at 15:35
  • 1
    This really should be an array if you want to access courses by index. Commented Mar 31, 2018 at 15:38
  • are you wanting to sort the courses? Commented Mar 31, 2018 at 15:39
  • 1
    What do you mean by "do something" and by "and so on"? Do you just want to rotate them? Commented Mar 31, 2018 at 15:39
  • w3schools.com/jsref/jsref_forin.asp Commented Mar 31, 2018 at 15:42

3 Answers 3

1

You can use Object.keys in order to get all an array of keys, and then you can iterate over it.

Something like that:

const person = {
  course1: {
    Name: "xxx",
    day: "mon",
    time: "2am-6pm"
  },
  course2: {
    Name: "yyy",
    day: "tue",
    time: "7am-9pm"
  },
  course3: {
    Name: "zzz",
    day: "tue",
    time: "2am-6pm"
  },
  course4: {
    Name: "aaa",
    day: "wed",
    time: "2am-6pm"
  },
  course5: {
    Name: "bbb",
    day: "thu",
    time: "2am-6pm"
  },
  course6: {
    Name: "ccc",
    day: "mon",
    time: "2am-6pm"
  }
};

Object.keys(person).forEach((key) => {
  const course = person[key];
  // Do your logic here...
});

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

Comments

1
const person = {
    course1:{Name:"xxx", day:"mon", time:"2am-6pm"},
    course2:{Name:"yyy", day:"tue", time:"7am-9pm"},
    course3:{Name:"zzz", day:"tue", time:"2am-6pm"},
    course4:{Name:"aaa", day:"wed", time:"2am-6pm"},
    course5:{Name:"bbb", day:"thu", time:"2am-6pm"},
    course6:{Name:"ccc", day:"mon", time:"2am-6pm"} 
};

const courses = Object.keys(person).filter(course => course !== 'course1');

courses.forEach(key => {
    const copyOne = Object.assign({}, person['course1']);
    const copyCurrent = Object.assign({}, person[key]);

    person['course1'] = copyCurrent;
    person[key] = copyOne;
});

console.log(person);

You want to compare with course1 on each iteration, so it doesn't make sense to use the key in forEach.

Comments

1
// find the least expensive course

// for easier coding, set a cost property for each course.
for(let course in person) {
    course['cost'] = getCourseCost(course);
}

let lowestCostCourse = person['course1'];

// this compares person['course1'] to itself, but so what
for(let course in person) {
    if(course['cost'] < lowestCostCourse['cost']) {
       lowestCostCourse = course;
    }
}

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.