Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Subtle · Jun 27, 2013 at 02:37 AM · javascriptarray

Obtaining a Array with Unique Items

Hello. I want a array with unique items. For example, {1, 2, 2, 3, ,4 ,4, 4}  => {1, 2, 3, 4} I found looking good javascript, but it doesn't work in unity script. And I'm not able to modify it. Could help me to modify this script?

 function unique(array) {
  var storage = {};
  var uniqueArray = [];
  var i,value;
  for ( i=0; i<array.length; i++) {
     value = array[i];
       if (!(value in storage)) {
        storage[value] = true;
          uniqueArray.push(value);
        }
    }
    return uniqueArray;
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BlueRaja_2014 · Jun 27, 2013 at 05:29 AM 0
Share

Can you use LINQ from Javascript? If so, you can just do array.Distinct()

avatar image Subtle · Jun 27, 2013 at 06:35 AM 0
Share

Oh, LINQ looks simple! But I can't understand how to use it although I studied it. Could you tell me more detail about it?

avatar image fafase · Jun 27, 2013 at 06:49 AM 0
Share

You simple need to add the reference at the top, probably

 import System.Linq;

and then you can use:

var res = array.Distinct();

now your res has no duplicate.

For more on Linq in Unity you can look there:

http://unitygems.com/linq-1-time-linq/

but it is in C#

avatar image SinisterRainbow · Jun 27, 2013 at 07:53 AM 0
Share

I would recommend mastering many more basic program$$anonymous$$g concepts first before using LINQ or finding tools because no matter how easy something is to use, you'll still never understand how to use it. Program$$anonymous$$g takes time and endless learning, if you're not up for it, you probably should consider something that interests you more.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SinisterRainbow · Jun 27, 2013 at 03:28 AM

This only is guaranteed to work when your original array is sorted (as in your example):

 var lastValue = Mathf.Infinity;
 for (i=0; i < array.length; ++i) {
  if (array[i] != lastValue) {
   uniqueArray.push(array[i]);
   lastValue = array[i];
  }
 }

I don't know Javascript well so I'm just copying your syntax, but the logic is sound (when sorted).. lastvalue remembers the last unique value from the original array, and so only uniques get placed in the new one. (Edit note: this is only partial code, i didn't copy all of the code from your example in case you are new to programming, i just did the important part)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Shotgun using array/list in Javascript 1 Answer

Convert Array into One String (Js) 1 Answer

Help me Convert JS to C# 2 Answers

Reading and Storing External Data into Memory (From Text) 1 Answer

Type 'Object' does not support slicing. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges