最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What is a CC++ data structure equivalent in Javascript? - Stack Overflow

programmeradmin0浏览0评论

Lets say I have the following in C

struct address{
   char name;
   int  id; 
   char address;
 };

struct address adrs[40];       //Create arbitrary array of the structure. 40 is example
adrs[0].name = 'a';
id[0]        = 1;
...

What is the equivalent way of defining and creating array of a user defined structure.

Thanks

Lets say I have the following in C

struct address{
   char name;
   int  id; 
   char address;
 };

struct address adrs[40];       //Create arbitrary array of the structure. 40 is example
adrs[0].name = 'a';
id[0]        = 1;
...

What is the equivalent way of defining and creating array of a user defined structure.

Thanks

Share Improve this question edited Oct 22, 2010 at 19:19 Mark K asked Oct 22, 2010 at 18:49 Mark KMark K 1,2443 gold badges20 silver badges31 bronze badges 1
  • It should be noted that JavaScript is prototyped based and weakly typed. You could create something like a struct by simply using an object literal, though. – Warty Commented Oct 22, 2010 at 18:57
Add a comment  | 

5 Answers 5

Reset to default 7

If you're going to have a predefined layout for an object, you'd probably want to use a contructor-style function.

function address() {
    this.name = null;
    this.id = null;
    this.address = null;
}

arrays are not typed and you don't have to specify a length.

var adrs = [];

you can create a new instance of address like so

var item = new address(); // note the "new" keyword here
item.name = 'a';
item.id = 1;
// etc...

then you can push the new item onto the array.

adrs.push(item);

alernatively you can add a new item from the array and then access it by indexer.

// adrs has no items
adrs.push( new address() );
// adrs now has 1 item
adrs[0].name = 'a';

// you can also reference length to get to the last item 
adrs[ adrs.length-1 ].id = '1';

Equivalent would be creating an array of associative arrays.

var arr = new Array();
arr[0] = { name: "name 1", id: 100, address: "addr 01" };
arr[1] = { name: "name 2", id: 101, address: "addr 02" };
//...

After this, you will be able to do:

arr[0].name = "new name 1";

Or access element:

if (arr[1].name == "name 2") { // will be true
}

Hope it helps.

Answer is veryyyy Simple.

const address = {
    name: ""
    id: 1,
    address: ""
}

Or Dynamic

const address = (name, id, address) => {
// Here your checks if enters is correct
    return {name, id, address}
}

If You Use TypeScript? It's so simple to.

interface address {
    name: string;
    id: number;
    address: string;
}

const adress: adress = {
    .....
}
<script type="text/javascript">

    function address() 
    {
       this.name="";
       this.id=0;
       this.address=""
    }
    var addresses = new Array(40);

    addresses[0] = new address();
    addresses[1] = new address();
    .....
    .....
    addresses[0].name = 'a';
    addresses[1].id = 5;


</script>

A common problem that is solved by structure is ranking system. An array that contains name and number of some users and then sorting users according to number. Here is javascript implementation of this problem.Object array is used here

<!DOCTYPE html>
<html>
<head>
	<title>Structure</title>
</head>
<body>
<label>Enter Student Name 1:</label>
<input type="text" id='n0'  name=""><br>
<label>Enter Student Mark 1:</label>
<input type="text" id='m0' name=""><br>
<label>Enter Student Name 2:</label>
<input type="text" id='n1' name=""><br>
<label>Enter Student Mark 2:</label>
<input type="text" id='m1' name=""><br>
<label>Enter Student Name 3:</label>
<input type="text" id='n2' name=""><br>
<label>Enter Student Mark 3:</label>
<input type="text" id='m2' name=""><br>
<input type="button" value="Ranking" onclick="result()">
<div id='id'></div>

<script type="text/javascript">
	
function result()
{
	var b=new Array(100);
	var n1=document.getElementById('n0').value;
	var m1=document.getElementById('m0').value;
	var n2=document.getElementById('n1').value;
	var m2=document.getElementById('m1').value;
	var n3=document.getElementById('n2').value;
	var m3=document.getElementById('m2').value;


	

	var a=new Array(100);
	var b=new Array(100);
	var n,m,j,i,temp,t,r="<br>Ranking<br><br>";

    for(i=0;i<3;i++)
    {

        n=document.getElementById('n'+i).value;
        m=document.getElementById('m'+i).value;
        m=parseInt(m);
    	a[i]={name:n,mark:m};
    }

    for(i=0;i<3;i++)
    {
 
      for(j=i+1;j<3;j++)
      {
    	if(a[j].mark>a[i].mark)
    	{
             temp=a[i].mark;
             t=a[i].name;
             a[i].mark=a[j].mark;
             a[i].name=a[j].name;
             a[j].mark=temp;
             a[j].name=t;

             //console.log(a[i].name);
    	     //console.log(a[i].mark);

    	}
      }
    }

    for(i=0;i<3;i++)
    {
        r=r+a[i].name+"  ";
        r=r+a[i].mark+"<br>";
    	//console.log(a[i].name);
    	//console.log(a[i].mark);
    }


    document.getElementById('id').innerHTML=r;

}

</script>
</body>
</html>

发布评论

评论列表(0)

  1. 暂无评论