-1

I'm a novice programmer just learning JSON and want to ask a question how to change data & print it

based on condition if score > 5, status changes from unposted to posted

let data = [
    {
        title: "The Only Guide You Need",
        score:8,
        status: "Posted"
    },
    {
        title: "The Advanced Guide To Archive",
        score:5,
        status: "Unposted"

    },
    {
        title: "In Defense of the Figurative Use of Literally.",
        score:6,
        status: "Unposted",
    },
]

console.log(data)

**thank you in advance

adtybrhn
  • 23
  • 6

1 Answers1

1

Just use Array.prototype.map to loop through each element. If the score is greater than 5, set status to "Posted"

let data = [
    {
        title: "The Only Guide You Need",
        score:8,
        status: "Posted"
    },
    {
        title: "The Advanced Guide To Archive",
        score:5,
        status: "Unposted"

    },
    {
        title: "In Defense of the Figurative Use of Literally.",
        score:6,
        status: "Unposted",
    },
]

data = data.map((current) => {
  if (current.score > 5) current.status = "Posted";
  return current;
});

console.log(data)
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34