Rearrange every other two keys in object

object.keys preserve order
javascript sort array of objects by number
sort object javascript
sort object on keys
javascript iterate object keys in order
js alphabetize object keys
sort object by key name javascript
sorting json object keys

In what way can this object be manipulated to rearrange values.

let obj = [
  {foo: 10, bar: 20},
  ["apple"],
  {foo: 30, bar: 40},
  ["pear"],
  {foo: 50, bar: 60},
  ["orange"]
]

// The output should be: 
// { "apple": { foo: 10, bar: 20 }, "pear": {...}, "orange": {...} }

I've tried the solution as provided here: loop and combine every two items

But that doesn't output the desired output.

Use for loop

let obj = [{foo: 10, bar: 20},["apple"],{foo: 30, bar: 40},["pear"],{foo: 50, bar: 60},["orange"]];

var result = {};
for(let i = 0; i < obj.length; i+= 2) {
  result[obj[i+1][0]] = obj[i];
}
console.log(result);

Changing the order of the Object keys., if you want to manually reorder. simply create new object and assign values using old object. You can't order JavaScript object key/value pairs. always need to know all of the properties in your object (a maintenance nightmare). Now the two items you wanted ordered are in order, and the remaining  Description. Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually.

You can use .reduce to reduce this array into an object. See the logic below.

let obj = [{foo: 10, bar: 20},["apple"],{foo: 30, bar: 40},["pear"],{foo: 50, bar: 60},["orange"]];

const arr = obj.reduce((a, el, i, arr) => {
  if(el instanceof Array){
    a[el[0]] = arr[i-1];
  }
  return a;
}, {});

console.log(arr);

Map() to the rescue; adding order to Object properties, Object properties order. a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the  key is pressed then 77 ( ASCII value of right arrow key) is stored in ch and values of x and y become 1 and 0. respectively. Now value of i. increased by one and j remains as it. is, this make the circle to move by one coordinate in x direction. This process. is repeated again and again till any other arrow key is pressed.

You can use array.reduce:

var obj = [
  {foo: 10, bar: 20},
  ["apple"],
  {foo: 30, bar: 40},
  ["pear"],
  {foo: 50, bar: 60},
  ["orange"]
];

var result = obj.reduce((m, o, i) => (Array.isArray(o) && (m[o[0]] = obj[i - 1]), m), {});

console.log(result);

Array methods, That's natural, because delete obj.key removes a value by the key . let arr = [ 1 , 2 ] ; // create an array from: arr and [3,4] alert ( arr . concat ( [ 3 , 4 ] ) ) ; // 1,2,3 Other objects, even if they look like arrays, are added as a whole: the provided function and reorder them, all we need is to provide the fn which  Given a sorted array of positive integers, rearrange the array alternately i.e first element should be maximum value, second minimum value, third second max, fourth second min and so on. Expected time complexity: O (n). The idea is to use an auxiliary array. We maintain two pointers one to leftmost or smallest element and other to rightmost or

Bulletin, No other practice is safe, and the renter will not object to carrying his keys and the old system of two keys, each key bearing the same number as the number of This system also avoids rearranging locks and recutting keys after boxes are  Then what I am doing is running pairs of files through some algorithms to determine the similarity between each pair of files. I do not need to compare file 1 to file 2, and then file 2 to file 1 again, as I only need the 2 files to be compared once. But I do need every file to be compared to every other file once.

Math Experiences for Young Learners, Grades PK, Alternatively, let children sort all of the objects in a different way and then recount to verify the sets of equivalent values or direct them to find two bags that equal the quantity of objects in a third bag. Does the number of things change if the objects are rearranged? KE-804063 © Key Education Math Experiences 54  Object.keys called on non-object can be here, if people is empty array – vp_arth Nov 13 '14 at 5:38 I think your first version was a best answer, need only check array isn't empty. Current version is too complicated :) – vp_arth Nov 13 '14 at 5:51

Array.prototype.sort(), If compareFunction is supplied, all non- undefined array elements are sorted according to with respect to each other, but sorted with respect to all different elements. var numbers = [4, 2, 5, 1, 3]; numbers.sort(function(a, b) { return a - b; } Objects can be sorted, given the value of one of their properties. Gravity decreases to ___ of its original value if the distance between two objects doubles. one-fourth Newtons law of ___ graviations states that every object in the universe attracts every other object.

Comments
  • Aha. Meaning: the last i+= 2 means loop over every two iterations. Then the result is the second value ([obj[i+1][0]]) containing the first value (obj[i]). Why are the square brackets around the [obj[i+1][0]] exaclty?
  • @Remi That is to set a create a dynamic property in object. Since your property name is in a variable (In your case, array inside array), to get the value, you need to use square braces. Few references that might help you: [Property Accessors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors), Dot Notation vs Brackets
  • It seems that the obj.prototype.reduce method proposed by @void (and Faly) performs better. Not sure if the method I used to measure the performance is representative, but still. So I tend to accept that as the best answer. If you disagree let me know. (thanks for the info)
  • @Remi - reduce will perform n iterations and for loop will perform n/2 iterations. Hence, for loop has better performance.
  • You're right. Based on your input also created a JSPerf: jsperf.com/rearrange-every-other-two-keys-in-object/1. Result is that the for loop performs +/- 16% better.
  • I would not prefer this approach as there will be n/2 iterations that will not do anything
  • Interesting. Any advantages to the approach of Nikhil?
  • Sorry to say but your answer looks very similar to @void's answer.