复制对象中的前7个键到一个新对象中。

huangapple go评论115阅读模式
英文:

Copy first 7 keys in object into a new object

问题

In JavaScript, you can create a new object containing the first 7 keys of another object like this:

const originalObject = {
  "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
};

const newObject = {};
const keysToCopy = Object.keys(originalObject).slice(0, 7);

for (const key of keysToCopy) {
  newObject[key] = originalObject[key];
}

console.log(newObject);

This code will create a new object (newObject) with the first 7 keys from the originalObject.

英文:

As the title suggests, how do I make a new object containing the 7 first keys of another object using JS? 复制对象中的前7个键到一个新对象中。 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.

答案1

得分: 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 https://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop)

英文:

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 https://stackoverflow.com/questions/280713/elements-order-in-a-for-in-loop)

huangapple
  • 本文由 发表于 2020年1月7日 01:38:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/59616564.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定