Home > OS >  get original object from a sorted array
get original object from a sorted array

Time:09-11

I have a chunked array with nested objects what i'm doing is getting a value from the object then sort it in ascending order and it works fine. What I want is once I get the numbers sorted I want to get the object that the number was in then push it to a new array . How can I achieve this? Thanks in advance.

note: there might be duplicate total views so don't just try and match the sorted array with the object value because that will cause wrong sort order.

const myArr = [{
    "title": "Hanging Out At Pokimanes House!",
    "totalViews": "1.5M views",
    "postedAgo": "1 day ago",
    "video_id": "XlZlKiT1uZM",
    "video_thumbnail": "http://img.youtube.com/vi/XlZlKiT1uZM/hqdefault.jpg"
  }, {
    "title": "JiDion Gets A Haircut At The Worlds Largest Tennis Match",
    "totalViews": "207K views",
    "postedAgo": "3 days ago",
    "video_id": "Q5twKgxO8xg",
    "video_thumbnail": "http://img.youtube.com/vi/Q5twKgxO8xg/hqdefault.jpg"
  }, {
    "title": "JiDion Gives Away $10,000",
    "totalViews": "1M views",
    "postedAgo": "7 days ago",
    "video_id": "XdNoAcONMB4",
    "video_thumbnail": "http://img.youtube.com/vi/XdNoAcONMB4/hqdefault.jpg"
  }, {
    "title": "JiDion Simping For Valkyrae For 13 Minutes & 10 Seconds",
    "totalViews": "231K views",
    "postedAgo": "8 days ago",
    "video_id": "H-vqkCkBaOs",
    "video_thumbnail": "http://img.youtube.com/vi/H-vqkCkBaOs/hqdefault.jpg"
  }, {
    "title": "KSI Wants JiDion To Start Boxing?!",
    "totalViews": "384K views",
    "postedAgo": "9 days ago",
    "video_id": "XyCHANsiJrc",
    "video_thumbnail": "http://img.youtube.com/vi/XyCHANsiJrc/hqdefault.jpg"
  },{
    "title": "How Mr Beast Made JiDion Delete His Twitter!",
    "totalViews": "445K views",
    "postedAgo": "9 days ago",
    "video_id": "oyvjTFtTep0",
    "video_thumbnail": "http://img.youtube.com/vi/oyvjTFtTep0/hqdefault.jpg"
  }, {
    "title": "Try Not to Get Offended Challenge Ft. BlmDion",
    "totalViews": "195K views",
    "postedAgo": "10 days ago",
    "video_id": "F9XNkj5c_IU",
    "video_thumbnail": "http://img.youtube.com/vi/F9XNkj5c_IU/hqdefault.jpg"
  }, {
    "title": "This Video Might Get JiDion Cancelled",
    "totalViews": "309K views",
    "postedAgo": "10 days ago",
    "video_id": "ktLcFyTOhK0",
    "video_thumbnail": "http://img.youtube.com/vi/ktLcFyTOhK0/hqdefault.jpg"
  }, {
    "title": "Your Favorite YouTuber Doesn't Like You",
    "totalViews": "503K views",
    "postedAgo": "11 days ago",
    "video_id": "oAAmzJDvrEk",
    "video_thumbnail": "http://img.youtube.com/vi/oAAmzJDvrEk/hqdefault.jpg"
  }, {
    "title": "JiDion Gets IShowSpeed And Jake Lucky On The Phone Together! (BEEF SQUASHED??)",
    "totalViews": "225K views",
    "postedAgo": "12 days ago",
    "video_id": "0lfF8J2KwSs",
    "video_thumbnail": "http://img.youtube.com/vi/0lfF8J2KwSs/hqdefault.jpg"
  }
]

function convert(number) {
  var base = parseFloat(number);
  if (number.toLowerCase().match(/k/)) {
    return Math.round(base * 1000);
  } else if (number.toLowerCase().match(/m/)) {
    return Math.round(base * 1000000);
  } else if (number.toLowerCase().match(/b/)) {
    return Math.round(base * 1000000000);
  }
}

let viewsTracker = []
for (var i = 0; i < myArr.length; i  ) {
   viewsTracker.push(convert(myArr[i].totalViews.replace(' views', '')))
}

viewsTracker.sort(function(a, b) {
  if (a === Infinity)
    return 1;
  else if (isNaN(a))
    return -1;
  else
    return a - b;
});

//here print myArr but by the sort order
console.log(viewsTracker.reverse());

CodePudding user response:

You can custom sort your objects array directly, instead of processing the viewsTracker array:

const myArr = [{
    "title": "Hanging Out At Pokimanes House!",
    "totalViews": "1.5M views",
    "postedAgo": "1 day ago",
    "video_id": "XlZlKiT1uZM",
    "video_thumbnail": "http://img.youtube.com/vi/XlZlKiT1uZM/hqdefault.jpg"
  }, {
    "title": "JiDion Gets A Haircut At The Worlds Largest Tennis Match",
    "totalViews": "207K views",
    "postedAgo": "3 days ago",
    "video_id": "Q5twKgxO8xg",
    "video_thumbnail": "http://img.youtube.com/vi/Q5twKgxO8xg/hqdefault.jpg"
  }, {
    "title": "JiDion Gives Away $10,000",
    "totalViews": "1M views",
    "postedAgo": "7 days ago",
    "video_id": "XdNoAcONMB4",
    "video_thumbnail": "http://img.youtube.com/vi/XdNoAcONMB4/hqdefault.jpg"
  }, {
    "title": "JiDion Simping For Valkyrae For 13 Minutes & 10 Seconds",
    "totalViews": "231K views",
    "postedAgo": "8 days ago",
    "video_id": "H-vqkCkBaOs",
    "video_thumbnail": "http://img.youtube.com/vi/H-vqkCkBaOs/hqdefault.jpg"
  }, {
    "title": "KSI Wants JiDion To Start Boxing?!",
    "totalViews": "384K views",
    "postedAgo": "9 days ago",
    "video_id": "XyCHANsiJrc",
    "video_thumbnail": "http://img.youtube.com/vi/XyCHANsiJrc/hqdefault.jpg"
  },{
    "title": "How Mr Beast Made JiDion Delete His Twitter!",
    "totalViews": "445K views",
    "postedAgo": "9 days ago",
    "video_id": "oyvjTFtTep0",
    "video_thumbnail": "http://img.youtube.com/vi/oyvjTFtTep0/hqdefault.jpg"
  }, {
    "title": "Try Not to Get Offended Challenge Ft. BlmDion",
    "totalViews": "195K views",
    "postedAgo": "10 days ago",
    "video_id": "F9XNkj5c_IU",
    "video_thumbnail": "http://img.youtube.com/vi/F9XNkj5c_IU/hqdefault.jpg"
  }, {
    "title": "This Video Might Get JiDion Cancelled",
    "totalViews": "309K views",
    "postedAgo": "10 days ago",
    "video_id": "ktLcFyTOhK0",
    "video_thumbnail": "http://img.youtube.com/vi/ktLcFyTOhK0/hqdefault.jpg"
  }, {
    "title": "Your Favorite YouTuber Doesn't Like You",
    "totalViews": "503K views",
    "postedAgo": "11 days ago",
    "video_id": "oAAmzJDvrEk",
    "video_thumbnail": "http://img.youtube.com/vi/oAAmzJDvrEk/hqdefault.jpg"
  }, {
    "title": "JiDion Gets IShowSpeed And Jake Lucky On The Phone Together! (BEEF SQUASHED??)",
    "totalViews": "225K views",
    "postedAgo": "12 days ago",
    "video_id": "0lfF8J2KwSs",
    "video_thumbnail": "http://img.youtube.com/vi/0lfF8J2KwSs/hqdefault.jpg"
  }
]


// FIRST CONCAT ALL OBJECTS INTO A SINGLE ARRAY
//const theArr = myArr.reduce((a, b) => a.concat(b))

// IN THIS EXAMPLE, THERE IS NO NEED TO CONCAT ARRAYS, SINCE IT IS ALREADY FLAT
const theArr = myArr

function convert(number) {
  var base = parseFloat(number);
  if (number.toLowerCase().match(/k/)) {
    return Math.round(base * 1000);
  } else if (number.toLowerCase().match(/m/)) {
    return Math.round(base * 1000000);
  } else if (number.toLowerCase().match(/b/)) {
    return Math.round(base * 1000000000);
  }
}



function compare(a, b) {
  a.numTotalViews = convert(a.totalViews.replace(' views', ''))
  b.numTotalViews = convert(b.totalViews.replace(' views', ''))

  if (a.numTotalViews < b.numTotalViews) {
    return 1;
  }
  if (a.numTotalViews > b.numTotalViews) {
    return -1;
  }
  return 0;
}

theArr.sort(compare);

console.log(theArr);

CodePudding user response:

Il'll suggest to flatten the array, this code will not mutate the original array

const myArr = [
  [
    {
      title: "Hanging Out At Pokimanes House!",
      totalViews: "1.5M views",
      postedAgo: "1 day ago",
      video_id: "XlZlKiT1uZM",
      video_thumbnail: "http://img.youtube.com/vi/XlZlKiT1uZM/hqdefault.jpg",
    },
    {
      title: "JiDion Gets A Haircut At The Worlds Largest Tennis Match",
      totalViews: "207K views",
      postedAgo: "3 days ago",
      video_id: "Q5twKgxO8xg",
      video_thumbnail: "http://img.youtube.com/vi/Q5twKgxO8xg/hqdefault.jpg",
    },
    {
      title: "JiDion Gives Away $10,000",
      totalViews: "1M views",
      postedAgo: "7 days ago",
      video_id: "XdNoAcONMB4",
      video_thumbnail: "http://img.youtube.com/vi/XdNoAcONMB4/hqdefault.jpg",
    },
    {
      title: "JiDion Simping For Valkyrae For 13 Minutes & 10 Seconds",
      totalViews: "231K views",
      postedAgo: "8 days ago",
      video_id: "H-vqkCkBaOs",
      video_thumbnail: "http://img.youtube.com/vi/H-vqkCkBaOs/hqdefault.jpg",
    },
    {
      title: "KSI Wants JiDion To Start Boxing?!",
      totalViews: "384K views",
      postedAgo: "9 days ago",
      video_id: "XyCHANsiJrc",
      video_thumbnail: "http://img.youtube.com/vi/XyCHANsiJrc/hqdefault.jpg",
    },
  ],
  [
    {
      title: "How Mr Beast Made JiDion Delete His Twitter!",
      totalViews: "445K views",
      postedAgo: "9 days ago",
      video_id: "oyvjTFtTep0",
      video_thumbnail: "http://img.youtube.com/vi/oyvjTFtTep0/hqdefault.jpg",
    },
    {
      title: "Try Not to Get Offended Challenge Ft. BlmDion",
      totalViews: "195K views",
      postedAgo: "10 days ago",
      video_id: "F9XNkj5c_IU",
      video_thumbnail: "http://img.youtube.com/vi/F9XNkj5c_IU/hqdefault.jpg",
    },
    {
      title: "This Video Might Get JiDion Cancelled",
      totalViews: "309K views",
      postedAgo: "10 days ago",
      video_id: "ktLcFyTOhK0",
      video_thumbnail: "http://img.youtube.com/vi/ktLcFyTOhK0/hqdefault.jpg",
    },
    {
      title: "Your Favorite YouTuber Doesn't Like You",
      totalViews: "503K views",
      postedAgo: "11 days ago",
      video_id: "oAAmzJDvrEk",
      video_thumbnail: "http://img.youtube.com/vi/oAAmzJDvrEk/hqdefault.jpg",
    },
    {
      title:
        "JiDion Gets IShowSpeed And Jake Lucky On The Phone Together! (BEEF SQUASHED??)",
      totalViews: "225K views",
      postedAgo: "12 days ago",
      video_id: "0lfF8J2KwSs",
      video_thumbnail: "http://img.youtube.com/vi/0lfF8J2KwSs/hqdefault.jpg",
    },
  ],
];
function convert(number) {
  var base = parseFloat(number);
  if (number.toLowerCase().match(/k/)) {
    return Math.round(base * 1000);
  } else if (number.toLowerCase().match(/m/)) {
    return Math.round(base * 1000000);
  } else if (number.toLowerCase().match(/b/)) {
    return Math.round(base * 1000000000);
  }
}

const convertTotalViewsToNumber = (data) => {
  return data.flat().map((item) => {
    return {
      ...item,
      totalViewsAsNumber: convert(item.totalViews),
    };
    
  });
};

const sortArrayByKey = (data, key) => {
  return data.sort((a, b) => {
    return b[key] - a[key];
  });
};

console.log(sortArrayByKey(convertTotalViewsToNumber(myArr),"totalViewsAsNumber"));

  • Related