英文:
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? 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论