-2

As the title suggests, how do I make a new object containing the 7 first keys of another object using JS? :) This is the structure of the object I would like to copy the data from.

{"data":[{"id":2338785,"team1":{"id":10531,"name":"Just For Fun"},"team2":{"id":10017,"name":"Rugratz"},"result":"2 - 0","event":{"name":"Mythic Cup 5","id":5148},"format":"bo3","stars":0,"date":1578279271000},....],"last_update":1578329378792}

Let's say there are 100 keys like this one, and I only want to copy the 7 first ones into a new object in JS.

PixelBoii
  • 21
  • 2
  • 6
  • 2
    You must be aware, that keys in objects are not sorted. So you have to specify in which way you want to order your keys (e.g. Alphanumerical). – Snapstromegon Jan 06 '20 at 17:40
  • 2
    @Snapstromegon That's old. They have a defined traversal order in ES6: keys starting with a number are sorted first; anything else is sorted in the order it was added to the object. See https://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties –  Jan 06 '20 at 17:43
  • However order can vary per browser: https://www.stefanjudis.com/today-i-learned/property-order-is-predictable-in-javascript-objects-since-es2015/#the-common-misconception-quot-the-order-of-javascript-properties-cannot-be-guaranteed-quot-nding – kingsfoil Jan 06 '20 at 17:47
  • 1
    They're still not *semantically* ordered, *"first 7 keys"* doesn't have a useful meaning. – jonrsharpe Jan 06 '20 at 17:47
  • When you say "make a new object containing the first 7 keys" do you mean that you wish to copy both the key and value into a new object? – kingsfoil Jan 06 '20 at 17:49
  • @Amy in your linked answer it clearly represents the language spec as the order is well defined, but nearly every way to use the order (for...in, Object.leys() and JSON.stringify) are not required to respect that order. Also browsers still vary on their behavior. – Snapstromegon Jan 06 '20 at 17:50
  • @Snapstromegon My point was that in ES6 there is nuance to the statement `keys in objects are not sorted`. That was completely accurate in ES5, but in ES6 its only "mostly" true, with some caveats. The order the keys are added to the object is retained now in ES6. –  Jan 06 '20 at 17:59

1 Answers1

0

Well technically, you have only 2 keys in the given Object but if you mean the data object, here's what you can do.

const MyNewObject = Object.entries(YourObject)
const results = []

and a simple for loop

MyNewObject.forEach((pair,i) => {
    // for the 8th item it breaks and doesn't push it to results array
    if ( i === 7 ) break;
    results.push(pair)
}

OR u use slice instead :

// a little bit neater , and u call remove the empty results array above
const thisNewResult = MyNewObject.slice(0,6)

and finally the results is an array of key value pairs and you should do this code to make a new object from the results entries

const finalResults = Object.fromEntries(results)

Please notice that this may not be the Order you want since Object.Entries gives you the same order as the order of for in loop (for more info visit Elements order in a "for (… in …)" loop)

Ali Jixer
  • 86
  • 6