79911

JavaScript: flatten an array without using reduce library

Question:

I have a nested array named $scope.instruments, its contents are:

<strong>collapsed:</strong>

<a href="https://i.stack.imgur.com/Aii9N.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Aii9N.png" data-original="https://i.stack.imgur.com/Aii9N.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

<strong>expanded:</strong>

<a href="https://i.stack.imgur.com/Fh5Bz.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Fh5Bz.png" data-original="https://i.stack.imgur.com/Fh5Bz.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

I also have an object:

<a href="https://i.stack.imgur.com/qd8Xn.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/qd8Xn.png" data-original="https://i.stack.imgur.com/qd8Xn.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

This object contains 2 arrays: AttributeID and SPAttributeRefID.

Currently I am flattening the $scope.instruments array using reduce function:

$scope.instruments = $scope.instruments.reduce(function (result, instrument) { result[instrument.ID] = instrument; return result }, {});

Then I access the AttributeID of the object and assign it to a variable like this:

$scope.recordInstrument = $scope.instruments[data.AttributeID[0]].ID;

I would rather use a different method to obtain the same result. I read that an array can be flatten using a for loop, that it is a more efficient method of doing it. Unfortunately, what I have tried so far, is not giving me the same results.

var arrInstruments = $scope.instruments; var arrLength = arrInstruments.length; for (var i = 0; i < arrLength; i++) { console.log(arrInstruments[i]); }

Can someone give me a hand converting the code that uses the reduce function to use a loop and have the same result in the assignment of AttributeID?

Many thanks.

Answer1:

This code should do what you are looking for:

var arrInstruments = $scope.instruments; var arrLength = arrInstruments.length; var result = {} for (var i = 0; i < arrLength; i++) { result[arrInstruments[i].ID] = arrInstruments[i]; } // the result variable contains what you want. <blockquote>

However I really don't understand why you would want that.

</blockquote>

This screenshot you took:<br /><a href="https://i.stack.imgur.com/Aii9N.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/Aii9N.png" data-original="https://i.stack.imgur.com/Aii9N.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Is <strong>not</strong> a multidimensional array. It's only the way your console show you a very large array (so it will not need to "draw" everything).

Recommend

  • Get symbol by address (symbolicating binary, iOS build)
  • how to handle mongodb's E11000 duplicate key error in ruby
  • Spring AOP Logging Interceptor and JAXB issue
  • How to insert an Image in WORD after a bookmark using OpenXML
  • Hadoop shuffle uses which protocol?
  • finding greatest prime factor using recursion in c
  • how to reopen a class in gems
  • Imageloader not loading image on real device
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • How to use JavaScript to determine whether a file exists in a directory?
  • How do I access an unhandled exception in an MVC Error view?
  • Replace value with Factor in r data.table
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Javascript Callbacks with Object constructor
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Weird JavaScript statement, what does it mean?
  • Apache 2.4 - remove | delete | uninstall
  • php design question - will a Helper help here?
  • How to delete a row from a dynamic generate table using jquery?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • json Serialization in asp
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Benchmarking RAM performance - UWP and C#
  • Angular 2 constructor injection vs direct access
  • How to stop GridView from loading again when I press back button?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Django query for large number of relationships
  • Programmatically clearing map cache
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app
  • Net Present Value in Excel for Grouped Recurring CF
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?