开发者

What is a C/C++ data structure equivalent in Javascript?

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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜