在其他html页面上使用Javascript全局数组变得未定义(Javascript global array becomes undefined when used on other html page)
我已经在firstpage.hmtl的脚本中声明了一些全局变量以及全局数组,并且这些全局变量和数组在本页面的任何地方都可以访问,并且可以在此页面上正常工作。 我想在应用程序的所有页面上使用window.arr []的值,但它在所有其他应用程序页面上显示为未定义
当我从firstpage.html重定向到secondpage.html时,我尝试获取这些全局变量的值,但这里出现问题,因为只是window.name在第二页上工作,而不是每个全局变量的值都未定义
我已经为这些全局变量尝试了很多名字,但其中只有一个名字是window.name,除了一切都是未定义的。
firstpage.html
window.name = "john"; window.car = "BMW"; window.arr = []; $(".hotels").click(function () { arr.push('hotels'); console.log("name = " + window.name); //working, prints "name = john" console.log("car = " + window.car); //working, prints "car = BMW" console.log("arr = " + window.arr); //working, prints "arr = hotels" }
secondpage.html
$(document).ready(function () { console.log("arr = " + window.arr); //not working here, prints "arr = undefined" console.log("car = " + window.car); //not working here, prints "car = undefined" console.log("name = " + window.name); //working, prints "name = john" });
I have declared some global variables along with global array in my firstpage.hmtl's script and these global variables and array are accessible everywhere on this page and working fine on this page. And I want to use the values of window.arr[] on all pages of the application but it is showing undefined on all other pages of application
When I redirect from firstpage.html to secondpage.html then I try to get the values of these global variables but here the issue arise because just window.name is working on second page other than that every global variable's value is undefined
I have tried many names for these global variables but just one of them is working which is window.name other than that everything is undefined.
firstpage.html
window.name = "john"; window.car = "BMW"; window.arr = []; $(".hotels").click(function () { arr.push('hotels'); console.log("name = " + window.name); //working, prints "name = john" console.log("car = " + window.car); //working, prints "car = BMW" console.log("arr = " + window.arr); //working, prints "arr = hotels" }
secondpage.html
$(document).ready(function () { console.log("arr = " + window.arr); //not working here, prints "arr = undefined" console.log("car = " + window.car); //not working here, prints "car = undefined" console.log("name = " + window.name); //working, prints "name = john" });
原文:https://stackoverflow.com/questions/48409406
最满意答案
这是一个稍微优化的版本,其中对象映射到只有匹配产品的对象或
null
,然后仅针对非null
进行过滤:class Products extends React.Component { getFilteredProducts() { const filter = this.props.filter; const filteredProducts = this.props.products.map(obj => { // filter out those that don't match const filtered = Object.values(obj.products) .filter(p => p.indexOf(filter) > -1); // if no match found, exclude from the list entirely if (filtered.length === 0) return null; // combine into a new object with only filterd products return { ...obj, ...{ products: filtered } }; }) // filter out objects without any matched products .filter(product => product); return filteredProducts; } render() { return ( <div> {this.getFilteredProducts() .map((product, ind) => <div key={ind}> {product.category} {Object.values(product.products).map(name => <li>{name}</li>)} </div> ) } </div> ) } }
Here is a slightly optimized version where objects are mapped to objects with only matched products or
null
s and then filtered out for only non-null
s:class Products extends React.Component { getFilteredProducts() { const filter = this.props.filter; const filteredProducts = this.props.products.map(obj => { // filter out those that don't match const filtered = Object.values(obj.products) .filter(p => p.indexOf(filter) > -1); // if no match found, exclude from the list entirely if (filtered.length === 0) return null; // combine into a new object with only filterd products return { ...obj, ...{ products: filtered } }; }) // filter out objects without any matched products .filter(product => product); return filteredProducts; } render() { return ( <div> {this.getFilteredProducts() .map((product, ind) => <div key={ind}> {product.category} {Object.values(product.products).map(name => <li>{name}</li>)} </div> ) } </div> ) } }
相关问答
更多-
TCP/IP模型是一个________。[2023-10-02]
a -
如果没有匹配的b.Description可以使用DefaultIfEmpty重载来使用b.Description : var descriptions = a .Select(x => x.TestBCollection .Where(b => b.FkTestAId == x.TestAId) .Select(b => b.Description) .DefaultIfEmpty(x.Description) .First()); ...
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
首先,您需要使用{}然后将if-else语句放在map主体中,并返回结果。 写这样: {navbarValue.map((obj, index) => { if (obj.Status) return
return })} 或者使用三元运算符 ... -
这是一个稍微优化的版本,其中对象映射到只有匹配产品的对象或null ,然后仅针对非null进行过滤: class Products extends React.Component { getFilteredProducts() { const filter = this.props.filter; const filteredProducts = this.props.products.map(obj => { // filter out those that don' ...
-
树枝渲染的映射对象(Twig rendering of a mapped objects)[2023-03-13]
我确实留下了一些拼写错误(我刚刚纠正了它们)。 我碰巧找到了解决方案。 我的实体映射方式存在问题。 这是我发现一些有用的提示http://obtao.com/blog/symfony2-issues-you-do-not-understand/ I left some typos indeed (i just corrected them). I happen to have found the solution. I had issues with the way my entities were map ... -
我认为这里有一个重要的问题,你是否真的想要拉姆达的行为。 如果我们用Ramda做这样的事情, 它不会改变你的数据 。 它会返回与原件共享他们所能分享的新对象,但原件仍然保持原样。 拉姆达团队(免责声明:我是拉姆达作者之一)认为这是一件非常好的事情。 但有时候可能会令人惊讶。 拉姆达没有任何现成的解决方案,这将使这一切变得简单。 如果我要这样做,我可能会首先分两步:删除目标动画,然后更新所有项的类属性。 这让我觉得更简单。 如果事实证明有性能问题,我可能会考虑将它们结合起来。 以下是一种方法: const { ...
-
如何使用Java 8流为映射值获取List的嵌套分组(How to get nested grouping for List with mapped values with Java 8 streams)[2022-10-08]
您可以使用Collectors.mapping将A实例映射到getC() : Map>> results = list.stream() .collect(groupingBy(A::getA, groupingBy(A::getB, mapping(A::getC, toList()) ... -
如果您可以为减速器发布相关代码,将会有所帮助。 但是,绝大多数情况下,组件未更新的原因是因为您直接在reducer中更改状态,而不是不可变地返回修改后的副本。 有关详细信息,请参阅http://redux.js.org/docs/FAQ.html#react-not-rerendering上的Redux FAQ答案。 更新 : 这一行看起来很可疑: playlists[source][idxToUpdate] = playlistUpdated; 请记住,如果要不可更新地更新嵌套数据,则必须将复制和更新的项 ...
-
尝试将Name子属性名称外部化为ExpressionAttributeNames。 Expression expr = new Expression(); expr.ExpressionStatement = "Pet.#name = :name"; expr.ExpressionAttributeNames["#name"] = "Name"; expr.ExpressionAttributeValues[":name"] = "Lilleper"; Try externalizing the Name ...