Copy first 7 keys in object into a new object [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
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.

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)

Related

How to concatenate two arrays without changing order in javascript [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
Improve this question
I have 3 arrays. Lets say the first array has 3 elements, second array has 2 elements, and third array has 5 elements. When I concatenate them the array[3] will go to first element of the second array, array[6] will go to the second element of third array, because I first concatenate the first array with the second. If I concatenate the first array with the third, then concatenate the second array, array[3] will point to the first element of the third array.
don't understand your content, but according do your subject , I think below may help.
const a = [1,2,3]
const b = [4,5]
const c = [...a, ...b] // [1,2,3,4,5]

How to get data in object without looping [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I want to get data from the object without for loop
this is my object
data = [{name:"A",value:[java:45,c++:50]},{name:"B",value:[java:12,c++:47]},{name:"C",value:[java:15,c++:32]}]
Expected result:
result_name = ["A","B","C"]
result_java = [45,12,15]
The structure of your array data is invalid. You wrote nested array value in square brackets yet the items are written as key-value pairs.
There is no magical way you can obtain multiple data from an array without iteration/looping. (unless you already know the indices, but this defeats the purpose of using arrays)
That said, if you meant to avoid only the "for" loops, you can:
Fix your data as below.
let data = [
{name:"A", value:{java:45,"c++":50}},
{name:"B", value:{java:12,"c++":47}},
{name:"C", value:{java:15,"c++":32}}
];
Run an alternative loop such as below.
console.log(data.map(item => item.name)); // ["A", "B", "C"]
console.log(data.map(item => item.value.java)); // [45,12,15]

Unusual array with key value NaN property, containing more arrays? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have an unusual Javascript array with a length of 0, but if I expand the array I can see it contains multiple nested arrays with the same property, and eventually an Array containing an object. I've never seen this type of array / object before.
This is a sudo code version (as far I can read it):
[NaN: [{foo:'barr'}]]
But constructing an object like that would cause an error. Can anyone explain what it is?
This is a screen shot of what it looks like console logged in Chrome:
And console logged in Firefox:
It looks like, you have 'NaN' as key, which is possible (arrays are objects), because you may have calculated the index, which goes wrong.
var array = [];
array['x' * 3] = 'value'; // index/key is NaN
console.log(array);
console.log(array.NaN);

What is the best way to group data pairs in javascript? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
Instead of using associative arrays (objects), what is the best way to associate two values so they are always associated and can both be accessed as separate values? Associative arrays are annoying because their order isn't guaranteed and you can't access them using indexes.
I could create two separate arrays but if I randomize their order for display their association would not match, and if they are separate in code there is a good chance I can make a mistake when recording their values, putting them in the wrong order so they don't perfectly match up.
In Javascript you can use multidimensional arrays:
var data = [
[1, 2],
[3, 4]
];
data[0][0]; // get '1'
data[0][1]; // get '2'
Also if you need to associate data in a more abstract way, ES6 has WeakMaps:
var wm = new WeakMap();
var x = document.createElement("div");
wm.set(x, {foo: 1, bar: 7});
console.log(wm.get(x).bar) // get '7' from object you associated to HTMLElement
I do not now which kind of data you should store but maybe you could try to store data in a single variable in this way:
var data = [];
data[0] = "Test" + "|" + Value;

Object Obect array Json.stringify string array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I had a object object array which i used JSON.stringify() on i can now see what's in my array but when i do arr[0] etc it only outputs one letter.
arr = {"hello":"yes","because":"no"}
arr[0] =h
I want it to output the whole of the value not just the first letter
My code
var clientContext = SP.ClientContext.get_current();
var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
// Get user properties for the target user.
// To get the PersonProperties object for the current user, use the
// getMyProperties method.
MyProperties = peopleManager.getMyProperties();
// Load the PersonProperties object and send the request.
clientContext.load(MyProperties);
clientContext.executeQueryAsync(getMyNewsChoicesSuccess, getMyNewsChoicesFail);
},
getMyNewsChoicesSuccess = function () {
//get the news choice by actually fieldname
var MyChoices = JSON.stringify(MyProperties.get_userProfileProperties().Value);
$('#NBStest').text(MyChoices);
},
You can get the first element from your json string like this
JSON.parse(json_str)[0]
but in the example you have, the first element is "yes" and its index is "hello" , which means you can't get the first element by the index 0 , however you can get it by its property name like this
arr.hello = "yes";
// or
arr['hello'] = "yes";
if you want to get the hello which is the key , you have to use this loop
for (key in arr)
console.log(key);
// it will print 'hello' and then 'because'
Well its not an array anymore, its a string. arr[0] will return the first letter.
If you want to get the objects from it you need to parse it ( try JSON.parse )
JSON.stringify() does exactly what it sounds like. It turns the javascript object into a string. So when you do arr[0] you are getting the first letter in the string. You need to turn it back into a javascript object if you want to get the actual values.

Categories

Resources